gpt4 book ai didi

java - 尝试从 sqlite 数据库填充 listview

转载 作者:行者123 更新时间:2023-12-01 12:30:35 28 4
gpt4 key购买 nike

我正在尝试使用数据库中的数据填充 ListView :

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visualizar);
EventoSQLHelper e = new EventoSQLHelper(this);
e.Open();
Cursor c = e.getRegistros();
String resp = "";

List<String> list = new ArrayList<String>();

ListView eventos = (ListView) findViewById(R.id.list_eventos);
TextView tx = new TextView(this);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
resp = c.getString(c.getColumnIndex("id"))
+ c.getString(c.getColumnIndex("descricao"))
+ c.getString(c.getColumnIndex("dataEvento"));
list.add(resp);
}
ArrayAdapter<String> adpt = new ArrayAdapter<String>(this,android.R.layout.activity_list_item,list);
eventos.setAdapter(adpt);
e.close();
}

但它给了我一个错误:

09-20 14:35:19.300: E/AndroidRuntime(29881): FATAL EXCEPTION: main
09-20 14:35:19.300: E/AndroidRuntime(29881): Process: com.example.pacixmobile, PID: 29881
09-20 14:35:19.300: E/AndroidRuntime(29881): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
09-20 14:35:19.300: E/AndroidRuntime(29881): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
09-20 14:35:19.300: E/AndroidRuntime(29881): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
09-20 14:35:19.300: E/AndroidRuntime(29881): at android.widget.AbsListView.obtainView(AbsListView.java:2715)
09-20 14:35:19.300: E/AndroidRuntime(29881): at android.widget.ListView.measureHeightOfChildren(ListView.java:1274)
09-20 14:35:19.300: E/AndroidRuntime(29881): at android.widget.ListView.onMeasure(ListView.java:1186)
09-20 14:35:19.300: E/AndroidRuntime(29881): at android.view.View.measure(View.java:17387)
09-20 14:35:19.300: E/AndroidRuntime(29881): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)

我想列出数据,然后我想单击任何项​​目,并向联系人列表发送短信。但我什至无法列出数据...我做错了什么?

最佳答案

java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

ArrayAdapter 需要一个只是 TextView 的资源。 android.R.layout.activity_list_item 是一个包含 ImageView 和 TextView 的 LinearLayout。请改用 android.R.layout.simple_list_item_1

关于java - 尝试从 sqlite 数据库填充 listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25951566/

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