由SimpleCursorAdapter继承/定制出来的Adapter

阅读更多
package com.CursorAdapterTest;

import static com.db.MyOpenHelper.*;

import com.db.MyOpenHelper;
import android.app.Activity;
import android.content.ContentValues;
//...
//...
//省略部分代码
package com.CursorAdapterTest;
import android.widget.TextView;

public class CursorAdapterTest extends Activity {

	/** Called when the activity is first created. */
	MyOpenHelper                   				 myHelper ;		       //声明MyOpenHelper对象
	int []                         				 testId ;			   //声明用于存放联系人id的数组
	String []                      				 testName ;		       //声明用于存放联系人姓名的数组
	String []                      				 testPhone ;	       //声明用于存放联系人电话的数组
	ListView                       			     lv;				   //声明ListView对象
	private static  SQLiteDatabase db          = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //得到ListView的布局
        lv = (ListView)findViewById(R.id.lv);

        //打开数据库
        myHelper = new MyOpenHelper(this, DB_NAME);
        db = myHelper.getWritableDatabase();
        /*ContentValues values = new ContentValues();

        for(int i=0;i<100;i++){
        	values.put(NAME,"A");
        	values.put(PHONE,1234567+i);
        	db.insert(TABLE_NAME, null, values);
        }*/
        //得到数据库的cursor
        Cursor c = db.query(TABLE_NAME, null, null, null, null, null, ID);
        startManagingCursor(c);

        //实例化adapter
        Adapter adapter = new Adapter(this,
        		R.layout.listitem,
        		c,
        		new String[]{ID,NAME,PHONE},
        		new int[]{R.id.list_Id,R.id.list_Name,R.id.list_Phone});
        //绑定adapter
        lv.setAdapter(adapter);
    }
    public class Adapter extends SimpleCursorAdapter {

    	private LayoutInflater mInflater;

    	public Adapter (Context context, int layout, Cursor c, String[] from, int[] to) {
    		        super(context, layout, c, from, to);
    		    }

		@Override
    	public void bindView(View view, Context context, Cursor cursor) {
			//得到每一个item的布局文件
    		LinearLayout ll = null;
    		if (view == null) {
    			ll = (LinearLayout) mInflater.inflate(R.layout.listitem,null);
    		} else {
    			ll = (LinearLayout)view;
    		}

    		//通过cursor得到每一个字段的ColumnIndex
    		int idCol = cursor.getColumnIndex(ID);
    		int nameCol = cursor.getColumnIndex(NAME);
    		int phoneCol = cursor.getColumnIndex(PHONE);

    		int nCount = cursor.getPosition();
    		//int nCount = Integer.valueOf(cursor.getString(idCol));

    		//得到每一个数据控件
    		ImageView iv_TView = (ImageView)ll.findViewById(R.id.list_Iv);
        	TextView id_TV = (TextView)ll.findViewById(R.id.list_Id);
        	TextView name_TV = (TextView)ll.findViewById(R.id.list_Name);
        	TextView phone_TV = (TextView)ll.findViewById(R.id.list_Phone);

        	//设置每一项数据内容
        	if( nCount % 2 == 0){
        		iv_TView.setImageDrawable(getResources().getDrawable(R.drawable.r));
        	} else {
        		iv_TView.setImageDrawable(getResources().getDrawable(R.drawable.b));
        	}
        	id_TV.setText(cursor.getString(idCol));
        	name_TV.setText(cursor.getString(nameCol));
        	phone_TV.setText(cursor.getString(phoneCol));
    	}
    }
}



	
	



		
		
			
			
			
		
  • 由SimpleCursorAdapter继承/定制出来的Adapter_第1张图片
  • 大小: 40.8 KB
  • 查看图片附件

你可能感兴趣的:(Android,XML)