试图从sqlite数据库填充listview

时间:2014-09-20 17:40:48

标签: java android sqlite 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)

我想列出数据,之后我想点击任何项目,然后将短信发送到联系人列表。但我甚至无法列出数据......我做错了什么?

1 个答案:

答案 0 :(得分:1)

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