listview中显示的android数据库显示一行

时间:2012-09-06 12:27:42

标签: android sqlite

我有一个数据库,我想在列表视图中显示。 我有一些工作要做。继承人代码:

Cursor cursor = myDataBase
                    .rawQuery(
                            "SELECT * FROM Accelerometer where cur_timestamp<'2012-09-04 11:36:25'",
                            null);          

            while (cursor.moveToNext()) {
                // Which column you want to exprort
                String arrStr[] = { cursor.getString(0)+" | " + cursor.getString(1)+" | " + cursor.getString(2)+" | "+ cursor.getString(3)+" | " + cursor.getString(4)+"\n" };
                if (arrStr.length != 0) {
                    Log.d("TravellerLog :: ", Arrays.toString(arrStr));
                    //data.setText(Arrays.toString(arrStr));
                    ListView listView = (ListView) findViewById(R.id.listView1);
                    // a listát berakni aegy arraylist-be
                     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1,
                             arrStr);
                     listView.setAdapter(adapter);

                } else {
                    Log.e("TravellerLog :: ", "Üres a tömb!");
                }
            }

            cursor.close();

我应该在代码中更改以使其工作?列出我想要的所有行 非常感谢你的答案!

2 个答案:

答案 0 :(得分:1)

l.setOnItemClickListener(new OnItemClickListener(){
    public void onItemClick(AdapterView<?> a, View v, int pos,long id){
        Cursor cursor = (Cursor) a.getItemAtPosition(pos);

        I.putExtra("ID", cursor.getInt(cursor.getColumnIndex("_id")));

       // more code here

连接数据库并使用select query ....

从数据库中获取值

答案 1 :(得分:0)

您正在初始化列表视图和while循环内的适配器。 这意味着对于每个数据库行,您都会创建一个新列表。

List<String> arrStr = new Vector<String>();
while (cursor.moveToNext()) {
            // Which column you want to exprort
            arrStr.add(cursor.getString(0)+" | " + cursor.getString(1)+" | " + cursor.getString(2)+" | "+ cursor.getString(3)+" | " + cursor.getString(4)+"\n" );
            if (arrStr.length != 0) {
                Log.d("TravellerLog :: ", Arrays.toString(arrStr));
                //data.setText(Arrays.toString(arrStr));
            } else {
                Log.e("TravellerLog :: ", "Üres a tömb!");
            }
        }

        cursor.close();

 ListView listView = (ListView) findViewById(R.id.listView1);
                // a listát berakni aegy arraylist-be
                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1,
                         arrStr);
                 listView.setAdapter(adapter);