gpt4 book ai didi

java - 在 Android 中使用 SimpleCursorAdapter 填充 ListView

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:40 24 4
gpt4 key购买 nike

我知道这个问题之前已经被问过和回答过,但是我已经按照多个教程进行操作都无济于事。

我只是想,如果我把我的代码放上去,也许有人可以帮助我并为我指明正确的方向。我不想撒谎,我在这方面是个菜鸟,但到目前为止我很喜欢它(好吧,到目前为止)。所发生的一切是当我加载 Activity 时,它只是强制关闭并显示以下错误消息:

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);
}

然后我的 ListView 代码:

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>

最佳答案

您遇到的错误很可能是因为您试图将 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

关于java - 在 Android 中使用 SimpleCursorAdapter 填充 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6348672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com