0

I have build an app that take data with different attribute, adds them in the database and then shows it through a ListView.

I have done the part where data are added, but I Can't figure out how to fetch it(for now I just want the name) from the database and populate it in the ListView.

Here is the part in the database class.

public Cursor getCursor() {
    Cursor c = null;
    sqLiteDatabase = this.getReadableDatabase();
    String query = "SELECT * FROM tbl_customer";
    String where = null;
    c = sqLiteDatabase.query("tbl_customer", new String[]{"Name"}, where, null, null, null, null);
    if (c != null) {
        c.moveToFirst();

    }
    return c;
}

here is the part of code in activity which I want to show the ListView in.

private void populateListView(){
    Cursor cursor = db.getCursor();
   String []From = new String[]{"Name"};

    int [] to = new int[R.id.textView];

    SimpleCursorAdapter adapter;
    adapter = new SimpleCursorAdapter(this,R.layout.listview_items,cursor,From,to,0);

    ListView listView = (ListView)findViewById(R.id.ShowDataListView);
    listView.setAdapter(adapter);
}

Please guide me, where I have gone wrong, and correct me.

3 Answers3

1

you are using R.id.textView as a size of array

int [] to = new int[R.id.textView];

it should be like this

int [] to = new int[]{R.id.textView};
Vivart
  • 13,137
  • 5
  • 32
  • 65
0

If you need a complete tutorial on how to create list and fetch it from sqlite database here is the tutorial from scratch http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

Moulesh
  • 2,510
  • 1
  • 17
  • 31
0

You've to specify the From and To params of the SimpleCursorAdapter.

adapter = new SimpleCursorAdapter(this,R.layout.listview_items,cursor,
new String[] { "Name" },to,0);

As for to, you need to put the id of your textview from R.layout.listview_items. Let's assume the id of your TextView is R.id.text1 then the adapter will look like following,

adapter = new SimpleCursorAdapter(this,R.layout.listview_items,cursor,
new String[] { "Name" },
new int[] { android.R.id.text1 },
0);

Here, have a look at the documentation to grasp it more clearly.

fluffyBatman
  • 5,304
  • 3
  • 19
  • 24
  • tnx for ur answer, but it didn't work. I keep getting this error: E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.OutOfMemoryError: int[] of length 2131230746 would overflow – Yama Raahi Aug 07 '16 at 11:59
  • In your getCursor method, you should close the database before returning the cursor. sqLiteDatabase.close(); return c; – fluffyBatman Aug 07 '16 at 12:08
  • that was a problem. but now I encountered the error that says "the column -id doesn't exist", Although I have a column ID, I tried to change the "ID" to "-id" and upgrade the database by changing its version but it seems that I am doing it wrong. – Yama Raahi Aug 08 '16 at 06:00
  • Drop the schema all together and create a new schema with refined column naming. See if its works. – fluffyBatman Aug 08 '16 at 06:17