在 Android 中使用 SimpleCursorAdapter 填充列表视图
我知道这个问题之前已经被问过并回答过,但我已经遵循了多个教程但无济于事。
我只是想如果我把我的代码放出来也许有人可以帮助并指出我正确的方向。我不会撒谎,我在这方面是个菜鸟,但到目前为止我很喜欢它(好吧,无论如何,到目前为止)。所发生的一切是当我加载活动时,它只是强制关闭并显示以下错误消息:
06-14 20:04:08.162: ERROR/AndroidRuntime(17605): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.magic8/com.example.magic8.AnswerList}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
所以,就是这样。我的数据库查询是:
public Cursor fetchAnswersList() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, "answer"},
null, null, null, null, null, null);
}
然后我的列表视图代码:
public class AnswerList extends Activity {
private Context context;
private DatabaseManager mDbHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.answerlist);
Cursor answers = mDbHelper.fetchAnswersList();
startManagingCursor(answers);
ListView answerList=(ListView)findViewById(R.id.answerList);
String[] from = new String[] {"_id", "answer"};
int[] to = new int[] {R.id.answerItem};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context, R.layout.list_item, answers, from, to);
answerList.setAdapter(cursorAdapter);
answerList.setOnItemClickListener(
new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
setToast("Answer: " + ((TextView) view).getText());
}
});
}
最后但并非最不重要的一点是,这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/addAnswerForm"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/addAnswer"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
<Button
android:text="Add"
android:id="@+id/addAnswer_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<LinearLayout
android:id="@+id/answers"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ListView
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/answerList"
android:layout_width="match_parent">
<TextView
android:id="@+id/answerItem"
android:layout_height="match_parent"
android:layout_width="match_parent">
</TextView>
</ListView>
</LinearLayout>
</LinearLayout>
I know this question has been asked and answered before, but I have followed multiple tutorials to no avail.
I just thought if I put my code up maybe someone could help and point me in the right direction. I am not going to lie, I am a massive noob at this but am loving it so far (well, up to this point anyway). All that is happening is when I load the activity, it just force closes with the following error message:
06-14 20:04:08.162: ERROR/AndroidRuntime(17605): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.magic8/com.example.magic8.AnswerList}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
So, here it is. My database query is:
public Cursor fetchAnswersList() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, "answer"},
null, null, null, null, null, null);
}
Then my list view code:
public class AnswerList extends Activity {
private Context context;
private DatabaseManager mDbHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.answerlist);
Cursor answers = mDbHelper.fetchAnswersList();
startManagingCursor(answers);
ListView answerList=(ListView)findViewById(R.id.answerList);
String[] from = new String[] {"_id", "answer"};
int[] to = new int[] {R.id.answerItem};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context, R.layout.list_item, answers, from, to);
answerList.setAdapter(cursorAdapter);
answerList.setOnItemClickListener(
new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
setToast("Answer: " + ((TextView) view).getText());
}
});
}
And last but not least, here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/addAnswerForm"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/addAnswer"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<requestFocus></requestFocus>
</EditText>
<Button
android:text="Add"
android:id="@+id/addAnswer_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<LinearLayout
android:id="@+id/answers"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ListView
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/answerList"
android:layout_width="match_parent">
<TextView
android:id="@+id/answerItem"
android:layout_height="match_parent"
android:layout_width="match_parent">
</TextView>
</ListView>
</LinearLayout>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您收到的错误很可能是因为您尝试将 TextView 嵌入 ListView...
从 ListView 中删除 TextView,然后尝试以下操作...
这使用预定义的 Android
android.R .layout.simple_list_item_1
和android.R.id.text1
The error you are getting is most likely because you're trying to embed a TextView into a ListView...
Remove the TextView from the ListView and then try the following...
This uses the pre-defined Android
android.R.layout.simple_list_item_1
andandroid.R.id.text1
您的往返长度必须相同。您不需要在“from”数组中包含 _id。请注意,您的光标中仍然需要有 _id。
Your to and from need to be the same length. You don't need to include _id in the 'from' array. Note that you still need to have _id in your cursor.