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));
}
}
}
<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<listview android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:dividerheight="1px" android:cachecolorhint="#00000000" android:smoothscrollbar="true">
</listview>
</linearlayout>
<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content">
<imageview android:id="@+id/list_Iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical">
<linearlayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0">
<textview android:id="@+id/list_Id" android:layout_width="wrap_content" android:layout_height="wrap_content">
<textview android:id="@+id/list_Name" android:layout_width="wrap_content" android:layout_height="wrap_content">
<textview android:id="@+id/list_Phone" android:layout_width="wrap_content" android:layout_height="wrap_content">
</textview></textview></textview></linearlayout>
</imageview></linearlayout>
- 大小: 40.8 KB
分享到:
相关推荐
SQLite数据库查询结果显示在ListView中,使用SimpleCursorAdapter
SimpleCursorAdapter简单实例
通过SQLiteDatabase.openDatabase方法可以打开某一绝对路径下的数据库,将数据库的内容放到listview上
简单的利用simplecursoradapter获取联系人信息
在很多时候需要将表中的数据显示在ListView、Gallery等控件中,虽然可以直接使用BaseAdapter进行处理,但工作量比较大,Android SDK提供了一个专用于数据绑定的Adapter:SimpleCursorAdapter。 SimpleCUrsorAdapter...
ListView的简单介绍与使用 GridView的简单介绍与使用 自定义BaseAdapter BaseAdapter的三种使用方式 ...BaseAdapter:抽象类,实际开发中我们会继承这个类并且重写相关方法,用得最多的一个Adapter!
android中的Adapter使用,比较常用的有 BaseAdapter,SimpleAdapter,ArrayAdapter,SimpleCursorAdapter等。
简单使用SimpleCursorAdapter
NULL 博文链接:https://2528.iteye.com/blog/838329
listView适配器SimpleAdapter和SimpleCursorAdapter细节总结
由图可以看到在Android中与Adapter有关的所有接口、类的完整层级图。在我们使用过程中可以根据自己的需求实现接口或者继承类进行一定的扩展。比较常用的有 BaseAdapter,SimpleAdapter,ArrayAdapter,...
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, new String[] { "_id" }, new int[] { android.R.id.text1 }); // 为桌号Spinner绑定数据 ...
simple-database4-cursor-adapter 通过 SimpleCursorAdapter 在 ListView 上显示 SQLite 记录
ListView 与ArrayAdapter,BaseAdapter,SimpleAdapter,SimpleCursorAdapter配合使用实例。
SimpleCursorAdapter adapter = new SimpleCursorAdapter( // 错误 MainActivity.this, R.layout.adapter_listview, cursor, new String[] { "title", "time" }, new int[] { R.id.text1, R.id....
代码如下:import android.app.ListActivity; import android.database.... import android.widget.SimpleCursorAdapter; /** * A list view example where the * data comes from a cursor, and a * SimpleCursorL
安卓开发-继承CursorAdapter实现自己的功能。这里就是往sqlite数据库插入数据,然后提取出数据并展示到listview中。这里仅仅是为了学习的目的。其实这里完全可以使用SimpleCursorAdapter来做。
SimpleCursorAdapter adapter = new SimpleCursorAdapter(context, R.layout.row, cursor, new String[]{"text"}, new int[]{R.id.text}, 0); list.setAdapter(adapter);而在...
android 适配器用法总结、比较常用的有 BaseAdapter,SimpleAdapter,ArrayAdapter,SimpleCursorAdapter等。