Java 在Android中使用SimpleCursorAdapter填充listview

Java 在Android中使用SimpleCursorAdapter填充listview,java,android,database,listview,simplecursoradapter,Java,Android,Database,Listview,Simplecursoradapter,我知道这个问题以前有人问过,也有人回答过,但我听过多个教程,都没有用 我只是想,如果我把我的代码放上去,也许有人能帮我找到正确的方向。我不想撒谎,我是一个大傻瓜,但到目前为止我很喜欢(好吧,到目前为止)。所发生的一切是当我加载活动时,它只是强制关闭,并显示以下错误消息: 06-14 20:04:08.162: ERROR/AndroidRuntime(17605): java.lang.RuntimeException: Unable to start activity ComponentInf

我知道这个问题以前有人问过,也有人回答过,但我听过多个教程,都没有用

我只是想,如果我把我的代码放上去,也许有人能帮我找到正确的方向。我不想撒谎,我是一个大傻瓜,但到目前为止我很喜欢(好吧,到目前为止)。所发生的一切是当我加载活动时,它只是强制关闭,并显示以下错误消息:

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());
        }
    });
}
public class AnswerList扩展活动{
私人语境;
私有数据库管理器mDbHelper;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.answerlist);
Cursor answers=mDbHelper.fetchAnswersList();
开始管理光标(答案);
ListView-answerList=(ListView)findViewById(R.id.answerList);
String[]from=新字符串[]{“\u id”,“answer”};
int[]to=newint[]{R.id.answerItem};
SimpleCursorAdapter cursorAdapter=新的SimpleCursorAdapter(上下文,R.layout.list_项,答案,from,to);
应答列表。设置适配器(游标适配器);
answerList.setOnItemClickListener(
新的MClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//单击后,显示带有文本视图文本的祝酒词
setToast(“回答:”+((TextView)视图).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>


您的“收件人”和“发件人”的长度必须相同。您不需要在“发件人”数组中包含_id。请注意,您的光标中仍然需要有_id。

您遇到的错误很可能是因为您试图将文本视图嵌入到列表视图中

<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>

这使用了预定义的Android
Android.R.layout.simple\u list\u item\u 1
Android.R.id.text1

您是否尝试使用ListActivity而不是Activity?您好,是的。我是否认为如果Activity中有多个列表,我需要使用Activity而不是ListActivity?目前只有一个列表t、 但是XML中有一个文本框和按钮,我希望能够允许用户将自己的答案添加到列表中。谢谢你们在发布后如此快速地回答我,我真的很感激。我现在有了以下代码,我仍然在强制关闭:
ListView answerList=(ListView)findViewById(R.id.answerList);String[]from=new String[]{“answer”};int[]to=new int[]{android.R.id.text1};SimpleCursorAdapter cursorAdapter=new SimpleCursorAdapter(context,android.R.layou.simple_list_item_1,answers,from,to);answerList.setAdapter(cursorAdapter);
My err msg:您的内容必须有一个id属性为“android.R.id.list”的ListView@javaMonkey:您是否将“活动”更改为ListActivity?如果是这样,您只是有点复杂。奇怪,现在我正在尝试并得到以下错误:
06-14 21:23:17.852:错误/AndroidRuntime(19652):java.lang.RuntimeException:无法启动活动组件信息{com.example.magic8/com.example.magic8.AnswerList}:java.lang.NullPointerException
。我已经研究了这意味着什么,但我看不到任何原因。@MisterSquonk不,我没有将其更改为ListActivity。@javaMonkey:尝试将xml文件中ListView的“id”更改为
android:id=“@android:id/list”
-您还必须为
findViewById>更改它(android.R.id.list)
非常感谢您对我的帮助,我真的很感激!!
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);