本文介绍了当使用SimpleCursorAdapter显示它改变的ListView项目文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当使用SimpleCursorAdaptor显示它如何改变项目的文本的ListView?
这里是我的code。
How to change text of items in ListView when it is displayed using SimpleCursorAdaptor?Here is my code.
Cursor allTaskcursor = databaseHelper.getAllTasks();
String[] from = {"name", "date"};
int[] to = new int[] {android.R.id.text1, android.R.id.text2};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context, android.R.layout.simple_list_item_2, allTaskcursor, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
allTaskListView.setAdapter(cursorAdapter);
的 getAllTasks(),它显示在的 android.R.id.text2 的的返回游标其中date是一个整数值(例如10)。我想改变的文本(例如10日)。
getAllTasks() returns a cursor where date is an Integer value (example 10) which is displayed in android.R.id.text2. I want to change that text (example "10 days").
推荐答案
的 SimpleCursorAdapter.ViewBinder 的做的工作。作为回答,我改变了code到..
SimpleCursorAdapter.ViewBinder did the job. As answered here, I changed the code to..
Cursor allTaskcursor = databaseHelper.getAllTasks();
String[] from = {"name", "date"};
int[] to = new int[] {android.R.id.text1, android.R.id.text2};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context, android.R.layout.simple_list_item_2, allTaskcursor, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == android.R.id.text2) {
int getIndex = cursor.getColumnIndex("date");
int date = cursor.getInt(getIndex);
TextView dateTextView = (TextView) view;
dateTextView.setText(date + " days");
return true;
}
return false;
}
});
allTaskListView.setAdapter(cursorAdapter);
这篇关于当使用SimpleCursorAdapter显示它改变的ListView项目文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!