在Android中使用SimpleCursorAdapter填充ListView

3
我知道这个问题以前已经被问过和回答过了,但我已经按照多个教程进行操作却没有成功。
我想如果我把我的代码放上来,也许有人可以帮忙并指引我正确的方向。不撒谎,我对此非常新手,但到目前为止还是很喜欢它(好吧,只是到这个点为止)。现在发生的一切就是当我加载活动时,它会强制关闭并显示以下错误消息:
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>

你尝试使用 ListActivity 而不是 Activity 了吗? - Harald Wilhelm
嗨,是的我有。如果活动中有多个列表,我需要使用Activity而不是ListActivity,这样想对吗?目前只有一个列表,但XML中有一个文本框和按钮,因为我想允许用户向列表中添加自己的答案。 - javaMonkey
2个回答

3
您遇到的错误很可能是因为您试图将TextView嵌入到ListView中...
<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>

从ListView中移除TextView,然后尝试以下操作...
String[] from = new String[] {"answer"};
int[] to = new int[] {android.R.id.text1};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context, android.R.layout.simple_list_item_1, answers, from, to);

这里使用预定义的Android android.R.layout.simple_list_item_1android.R.id.text1


@MisterSquonk:显然出错在Cursor answers = mDbHelper.fetchAnswersList();这行。我得到了mDbHelper = null的错误。 - javaMonkey
@javaMonkey:好的,看起来查询失败了。您是否已经验证了DATABASE_TABLE是您表格的正确名称,而KEY_ROWID"answer"是否为该表格列名的有效名称? - Squonk
@MisterSquonk:我已经研究过了,表格和字段都是正确的。我没有打开mDbHelper [mDbHelper.open()],因为我认为如果在主活动(Magic8Ball.java)中打开它,我就不必再次打开它。无论如何,我打开了它,然后出现了“列_id不存在”的错误。所以我将其添加到查询String query = "SELECT "+ KEY_ROWID +", answer FROM "+ DATABASE_TABLE;。我再次运行它,但我仍然得到NullPointerException错误,而且我似乎无法调试它,因为我得到了“源附件不包含文件...”的错误提示。 - javaMonkey
@javaMonkey:你的表格实际列名是什么? - Squonk
@MisterSquonk:我只有三列数据:1)"_id" 2)"answer" 3)"editable" - 表名为"answers"。 - javaMonkey
显示剩余11条评论

2

你的起始和结束位置需要相同长度。在'from'数组中不需要包含_id。请注意,你仍然需要在游标中包含_id。


就像感谢你帮助我一样,非常感激! - javaMonkey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接