50 Android fragment 输入_确定_更新

rightlist.xml (加载到 fragment)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <!-- 注意 id 的使用 -->
     <ListView android:id="@id/android:list"
               android:layout_width="match_parent"
               android:layout_height="0dp"
               android:background="#00FF00"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>
    

</LinearLayout>



RightFragment extends ListFragment

RightFragment.java  代码:

package com.example.android_fragment_loadermanager;

import java.util.ArrayList;
import java.util.List;

import android.app.ListFragment;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class RightFragment extends ListFragment implements LoaderCallbacks<Cursor>{

	private LoaderManager manager;
	private MyAdapter adapter;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		manager=getLoaderManager();
		adapter=new MyAdapter();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		View view=inflater.inflate(R.layout.rightlist, null);
		//实例化loader
		manager.initLoader(1001, null, this);
		//在这里加载 list
//		setListAdapter(new MyAdapter());
		
		return view;
	}
	/**
	 * 自定义适配器
	 * @author Administrator
	 *
	 */
	public class MyAdapter extends BaseAdapter{

		private List<String> list;
		
		public void bindData(List<String > list)
		{
			this.list=list;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView textview=null;
			if(convertView==null)
			{
				textview=new TextView(getActivity());
			}else{
				textview=(TextView) convertView;
			}
			
			textview.setText(list.get(position).toString());
			textview.setTextSize(25);
			return textview;
		}
		
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		// TODO Auto-generated method stub
		//uri
		CursorLoader loader=new CursorLoader(getActivity(), "content://com.example.....", null, null, null, null);
		
		return loader;
	}

	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
		// TODO Auto-generated method stub
		List<String> list=new ArrayList<String>();
		//如果有下一条
		while(arg1.moveToNext())
		{
			list.add(arg1.getString(arg1.getColumnIndex("name")));
		}
		//绑定数据
		adapter.bindData(list);
		//设置适配器
		setListAdapter(adapter);
		
		
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		// TODO Auto-generated method stub
		
	}
}


activity_main.xml  布局文件代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/left_main"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:background="#cccccc"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:hint="请输入姓名"
            >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="添加" />

    </LinearLayout>

    <LinearLayout
         android:background="#000cf6"
        android:id="@+id/right_main"
         android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

MainActivity.java 代码


package com.example.android_fragment_loadermanager;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

	private Button button;
	private EditText editText;
	private FragmentManager manager;
	private FragmentTransaction transaction;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button=(Button)this.findViewById(R.id.button1);
		manager=this.getFragmentManager();
		
		transaction=manager.beginTransaction();
		RightFragment fragment=new RightFragment();
		transaction.add(R.id.right_main, fragment, "right_fragment");
		transaction.commit();
		editText=(EditText)this.findViewById(R.id.editText1);
		
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String str=editText.getText().toString();
				ContentResolver resolver=getContentResolver();
				ContentValues values=new ContentValues();
				values.put("name", str);
				values.put("address", "xxxx");
				Uri uri=Uri.parse("content.......");
				resolver.insert(uri, values);
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}





你可能感兴趣的:(50 Android fragment 输入_确定_更新)