0

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

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

我应该在代码中进行哪些更改才能使其正常工作?列出我想要的所有行非常感谢您的回答!

4

2 回答 2

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

使用选择查询连接到数据库并从数据库中获取值....

于 2012-09-06T12:56:18.043 回答
0

您正在 while 循环内初始化列表视图和适配器。这意味着您为每个 db 行创建一个新列表。

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);
于 2012-09-06T12:30:05.607 回答