如何使用游标从我的数据库中提取数据以用于 ListView,但不必在 ListView 的列中显示该数据?
How can I extract data from my database with a Cursor for use in a ListView, but not have to display that data within a column in the ListView?
我正在使用游标适配器填充 ListView,就像所描述的那样 HERE:
DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
Cursor cursor = databaseHelper.getPeople();
String[] fromColumns = {"_id","firstname","lastname","email"};
int[] toViews = {R.id.count, R.id.firstname, R.id.lastname, R.id.email};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.person_name_and_email, cursor, fromColumns, toViews, 0);
ListView listView = getListView();
listView.setAdapter(adapter);
我的数据库 table 中还有另一列名为 "status",它是此人是否 "friend"、"family"、"business associate" 等。
我打算用这一栏的内容来决定人物行的颜色;如果此人是朋友,该行将为绿色,如果该人是家人,该行将为蓝色,依此类推
所以,我不希望 "status" 数据在 ListView 中显示为一列,但我需要它用于我迭代的每一行。
有办法吗?
谢谢!
尝试覆盖 getView() 方法
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, layout, cursor, fromColumns, toViews, 0);
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
cursor.moveToPosition(position);
String s = cursor.getString(cursor.getColumnIndex(“status"));
final View row = super.getView(position, convertView, parent);
if (s.equalsIgnoreCase(“friend"))
row.setBackgroundColor();
return row;
}
};
我正在使用游标适配器填充 ListView,就像所描述的那样 HERE:
DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
Cursor cursor = databaseHelper.getPeople();
String[] fromColumns = {"_id","firstname","lastname","email"};
int[] toViews = {R.id.count, R.id.firstname, R.id.lastname, R.id.email};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.person_name_and_email, cursor, fromColumns, toViews, 0);
ListView listView = getListView();
listView.setAdapter(adapter);
我的数据库 table 中还有另一列名为 "status",它是此人是否 "friend"、"family"、"business associate" 等。
我打算用这一栏的内容来决定人物行的颜色;如果此人是朋友,该行将为绿色,如果该人是家人,该行将为蓝色,依此类推
所以,我不希望 "status" 数据在 ListView 中显示为一列,但我需要它用于我迭代的每一行。
有办法吗?
谢谢!
尝试覆盖 getView() 方法
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, layout, cursor, fromColumns, toViews, 0);
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
cursor.moveToPosition(position);
String s = cursor.getString(cursor.getColumnIndex(“status"));
final View row = super.getView(position, convertView, parent);
if (s.equalsIgnoreCase(“friend"))
row.setBackgroundColor();
return row;
}
};