通过不同的文本视图

时间:2016-01-01 11:18:39

标签: android listview adapter

我有以下代码

public void populateListView() {
        Cursor cursordata = myDb.getAllData();
        ArrayList<String> products = new ArrayList<String>();


        Log.d(LOG_TAG, "cursor data " + cursordata);


        if (cursordata.moveToFirst()) {
            do {

                String productname = cursordata.getString((cursordata.getColumnIndex("product"))) +
                        cursordata.getString((cursordata.getColumnIndex("qty"))) +
                        cursordata.getString((cursordata.getColumnIndex("date_time")));

                products.add(productname);

            } while (cursordata.moveToNext());
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, products);
        ListView listviewman = (ListView)findViewById(R.id.listviewman);
       listviewman.setAdapter(adapter);
    }

它从列product,qty和date_time输出我的数据库中的列表, 但是,它看起来像一个字符串,它可以将产品,qty和date_time输出到自定义布局的不同文本视图中吗?

2 个答案:

答案 0 :(得分:0)

您可以构建自定义适配器。例如,自定义适配器看起来像:

find()

这里我们在ListView中的项目的不同TextView中设置文本。

答案 1 :(得分:0)

感谢大家的指示,我已经按照这个示例:http://www.mysamplecode.com/2012/07/android-listview-cursoradapter-sqlite.html

并设法使用以下代码完成任务:

Cursor cursor = db.getAllData();

        // The desired columns to be bound
        String[] columns = new String[]{
                db.productCOL,
                db.qtyCOL,
        };

        // the XML defined views which the data will be bound to
        int[] to = new int[]{
                R.id.productname,
                R.id.qty,
        };

        // create the adapter using the cursor pointing to the desired data
        //as well as the layout information
        dataAdapter = new SimpleCursorAdapter(
                this, R.layout.row_layout_history,
                cursor,
                columns,
                to,
                0);

        ListView listView = (ListView) findViewById(R.id.tutlist);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);