我有一个 ListView ,它从 sqlite 数据库加载数据。 listview 中的每一行都有 image 、 textview 和一个复选框。 sqlitedatabase 行有图像和文本数据以及一些其他列。
我的问题是我能否将 ListView 与数据库绑定(bind),以便所有行都自动加载所需数据。 (image + textview) 有一些示例可以绑定(bind)一个简单的 TextView 列表。复杂的行呢?也有一些微调器可以根据其值过滤列表中的数据。 (在我的数据库中充当 WHERE 子句)
目前,我通过为每一行的自定义适配器生成 View 来管理这一切。所以每次我查询数据库并填充数据。我保留最后一个 ListView 结果,根据微调器值等操作/条件生成更新的结果,然后通知数据更改到适配器以加载我的新结果。
要添加 DELETE 、 ADD 、 SEARCH 等功能,我必须使用集合来管理它。
有什么简单的方法吗?好像数据库很大,那么在内存中保存如此庞大的结果集的方法并不好。并且管理起来很痛苦。 谢谢。
最佳答案
这是我的行示例,由来自 db + 图像的两条记录构成(目前 - 任何行一个图像,但可以针对来自 db 的特定图像进行改进):
public class DomainAdapter extends SimpleCursorAdapter{
private Cursor dataCursor;
private LayoutInflater mInflater;
....
public DomainAdapter(Context context, int layout, Cursor dataCursor, String[] from,
int[] to) {
super(context, layout, dataCursor, from, to);
this.dataCursor = dataCursor;
mInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(layout, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.test_track);
holder.text2 = (TextView) convertView.findViewById(R.id.test_band);
holder.icon = (ImageView) convertView.findViewById(R.id.test_artwork);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
// Cursor to current item
dataCursor.moveToPosition(position);
int title_index = dataCursor.getColumnIndex(fields[0]);
String title = dataCursor.getString(title_index);
int description_index = dataCursor.getColumnIndex(fields[1]);
String description = dataCursor.getString(description_index);
holder.text1.setText(title);
holder.text2.setText(description);
holder.icon.setImageResource(R.drawable.alert_dialog_icon);
return convertView;
}
static class ViewHolder {
TextView text1;
TextView text2;
ImageView icon;
}
}
并使用这个适配器:
databaseListAdapter = new DomainAdapter(this,
R.layout.test_layout,
databaseCursor,
new String[] {"title", "description"},
new int[] { R.id.test_track, R.id.test_track });
databaseListAdapter.notifyDataSetChanged();
DomainView.setAdapter(databaseListAdapter);
和布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="64dip"
android:padding="6dip">
<TextView
android:id="@+id/test_band"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_below="@+id/test_track"
android:layout_alignLeft="@id/test_track"
android:layout_alignParentBottom="true"
android:gravity="top" />
<TextView
android:id="@id/test_track"
android:layout_marginLeft="6dip"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_toRightOf="@+id/test_artwork"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="bottom" />
<ImageView
android:id="@id/test_artwork"
android:layout_width="56dip"
android:layout_height="56dip"
android:layout_gravity="center_vertical" />
</RelativeLayout>
关于android - 将 listview 行与自定义行的 sqlite 数据库绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4520524/