0

i've got a database what i wanted to display in a listview. i have something to work. heres the code:

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

what should i change in the code to get it work?To list all the rows i want Thank you very much for the answers!

2 Answers 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

connect to database and get the value from database by using select query....

Sign up to request clarification or add additional context in comments.

Comments

0

You are initializing the list view and the adapter inside the while loop.. which means that for every db row you create a new list.

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

1 Comment

how can i put it out from the while loop?bacause somehow i try it i cant :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.