Android ListView-自定义适配器-数据库
安卓2.3.3 我在数据库中有一个表,有两列。我希望检索这些数据并在具有两个文本视图(第一个文本视图中的第一列和第二个文本视图中的第二列)的列表视图中显示它们,并对数据库中的所有行重复此操作。我读了一些关于(自定义适配器和listview)如何操作的示例,但是读得越多,我就越感到困惑 有人能告诉我怎么做吗?我可以使用动态列表视图,但我希望使用静态列表视图 以下是listview中的行布局:Android ListView-自定义适配器-数据库,android,android-listview,custom-adapter,Android,Android Listview,Custom Adapter,安卓2.3.3 我在数据库中有一个表,有两列。我希望检索这些数据并在具有两个文本视图(第一个文本视图中的第一列和第二个文本视图中的第二列)的列表视图中显示它们,并对数据库中的所有行重复此操作。我读了一些关于(自定义适配器和listview)如何操作的示例,但是读得越多,我就越感到困惑 有人能告诉我怎么做吗?我可以使用动态列表视图,但我希望使用静态列表视图 以下是listview中的行布局: <?xml version="1.0" encoding="utf-8"?> <Line
<?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="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<TextView
android:id="@+id/txtView_History_Expression"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="Expression"
android:textColor="#FFFFFF"
/>
<TextView
android:id="@+id/txtView_History_Result"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="Result"
android:textColor="#316DA2" />
</LinearLayout>
以下是包含ListView的XML:
<?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" >
<TextView
android:id="@+id/txtView_History_Header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HISTORY"
android:gravity="center"
android:textSize="24dp"
android:textColor="#316DA2"
android:paddingTop="10dp"
/>
<ListView
android:id="@+id/lvHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
我从数据库中检索数据并将其存储在游标中。我想从游标读取数据,并将这些值赋给列表视图中的文本视图。您应该遵循此流程 步骤1->从数据库获取数据,并用getter Setter填充列表 步骤2->创建BaseAdapter类并在该类中给出该列表[这里您需要查看BaseAdapter绑定方法]
Stap 3->将BaseAdapter对象与ListView绑定您应该遵循以下流程 步骤1->从数据库获取数据,并用getter Setter填充列表 步骤2->创建BaseAdapter类并在该类中给出该列表[这里您需要查看BaseAdapter绑定方法] Stap 3->将BaseAdapter对象与ListView绑定 我在数据库中有一个表,有两列。我想找回这个 数据,并在具有两个文本视图的列表视图中显示它们(中的第一列) 第一个文本视图和第二个文本视图中的第二列)并重复此操作 数据库中所有行的截止日期。我读了几个例子 关于(自定义适配器和listview)如何做,但我读得越多 我越来越糊涂了 你好。这个算法不太复杂。您需要执行以下步骤:
列表
或直接光标
ListAdapter
的子类以更好地控制它ListAdapter
设置为ListView
public List<DataType> getAll() {
List<DataType> objects = new ArrayList<DataType>();
DataType child = null;
Cursor c = null;
try {
String query = "select * from TableName";
c = db.rawQuery(query, null);
if (c.moveToFirst()) {
child = new DataType();
child.setId(c.getInt(c.getColumnIndex("id")));
child.setName(c.getString(c.getColumnIndex("name")));
objects.add(child);
}
return objects;
}
finally {
if (c != null) {
c.close();
}
if (db != null) {
db.close();
}
}
}
RowHolder
可能看起来像
public class RowHolder {
private View row;
private TextView idColumn;
public RowHolder(View v) {
this.row = v;
}
public TextView getIdColumn() {
if (idColumn == null) {
idColumn = (TextView) row.findViewById(R.id.idColumnId);
}
return idColumn;
}
}
最后将适配器设置为ListView
,工作就完成了
注:
DataType
是自己定义的对象。它显示数据库中的表,其中对象的属性与表中的列相同
db Cursor还有其他方法和返回,使用SimpleCursorAdapter
或CursorAdapter
。以下是一些很好的例子:
列表
或直接光标
ListAdapter
的子类以更好地控制它ListAdapter
设置为ListView
public List<DataType> getAll() {
List<DataType> objects = new ArrayList<DataType>();
DataType child = null;
Cursor c = null;
try {
String query = "select * from TableName";
c = db.rawQuery(query, null);
if (c.moveToFirst()) {
child = new DataType();
child.setId(c.getInt(c.getColumnIndex("id")));
child.setName(c.getString(c.getColumnIndex("name")));
objects.add(child);
}
return objects;
}
finally {
if (c != null) {
c.close();
}
if (db != null) {
db.close();
}
}
}
RowHolder
可能看起来像
public class RowHolder {
private View row;
private TextView idColumn;
public RowHolder(View v) {
this.row = v;
}
public TextView getIdColumn() {
if (idColumn == null) {
idColumn = (TextView) row.findViewById(R.id.idColumnId);
}
return idColumn;
}
}
最后将适配器设置为ListView
,工作就完成了
注:
DataType
是自己定义的对象。它显示数据库中的表,其中对象的属性与表中的列相同
db Cursor还有其他方法和返回,使用SimpleCursorAdapter
或CursorAdapter
。以下是一些很好的例子:
- 你所能做的就是看一看。
同时,请检查
我希望这将指导你
谢谢。你所能做的就是看看。 同时,请检查 我希望这将指导你 谢谢