完成记事本教程后,我设法在编辑笔记部分添加了一个评分栏,允许用户与之交互并对笔记评分,这被保存在 SQL 数据库中。现在,我尝试在 fillData()
方法中添加评级栏,该方法定义列表的行元素,允许使用数据库中的条目轻松填充列表。但问题是程序因 R.id.rating
而崩溃,我不知道如何修复它。有没有办法在主 ListView 中显示评分和注释?谢谢你
private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
// Create an array to specify the fields we want to display in the list
String[] from = new String[]{NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_BODY,NotesDbAdapter.KEY_RATING};
// and an array of the fields we want to bind those fields to
int[] to = new int[]{R.id.text1,R.id.text2,R.id.rating};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
setListAdapter(notes);
}
最佳答案
SimpleCursorAdapter
只支持TextViews
和ImageViews
不支持RatingBar
详情引用:
http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
现在要在 ListView
中显示 RatingBar
,您必须使用 CursorAdapter
并使用它的 newView
来创建和bindView
设置评分值。您还可以使用使用起来更冗长的 ListAdapter
。
引用:
http://developer.android.com/reference/android/widget/CursorAdapter.html http://developer.android.com/reference/android/widget/ListAdapter.html
关于android - 在 ListView 中显示 RatingBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8379512/