Leace
Leace

Reputation: 262

Android: Populating a GridtView from the SQLite Database

Gridview is not populating any data from Sqlite database while saving the data in to database. Logcat is not generating any error also.

My DB is.

public Cursor getAllRows() {
  SQLiteDatabase db = this.getReadableDatabase();
  //String where = null;
  Cursor c = db.rawQuery("SELECT  * FROM " + DATAALL_TABLE, null);
  if (c != null) {
    c.moveToFirst();
  }
  return c;
}

Mainactivity.

public void populateListView() {

  Cursor cursor = db.getAllRows();
  String[] fromFieldNames = new String[] {
    DBHelper.COURSES_KEY_FIELD_ID1, DBHelper.FIELD_MATERIALDESC, DBHelper.FIELD_MATERIALNUM
  };
  int[] toViewIDs = new int[] {
    R.id.textView1, R.id.textView3, R.id.textView2
  };
  SimpleCursorAdapter myCursorAdapter;
  myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), android.R.layout.activity_list_item, cursor, fromFieldNames, toViewIDs, 0);
  GridView myList = (GridView) findViewById(R.id.gridView1);
  myList.setAdapter(myCursorAdapter);

}

Post to the honorable member @MikeT advise its works fine but need alignment ,

as is

enter image description here

expected format

enter image description here

Upvotes: 1

Views: 66

Answers (1)

MikeT
MikeT

Reputation: 57103

Your issue, assuming that there is data in the table, is likely that R.id.textView1 .... 3 are nothing to do with the layout passed to the SimpleCursorAdapter. i.e. Your issue is likely to do with the combination of the layout passed to the SimpleCursorAdapter and the Id's of the views passed as the to id's.

If you were to use :-

    gridview = this.findViewById(R.id.gridView1);
    csr = DBHelper.getAllRows();
    myCursorAdapter = new SimpleCursorAdapter(
            getBaseContext(),
            //android.R.layout.simple_list_item_2,
            android.R.layout.activity_list_item,
            csr,
            new String[]{
                    SO50674769DBHelper.COURSES_KEY_FIELD_ID1
                    //SO50674769DBHelper.FIELD_MATERIALDESC,
                    //SO50674769DBHelper.FIELD_MATERIALNUM
            },
            new int[]{android.R.id.text1}, //<<<< ID of the available view
            0
    );
    gridview.setAdapter(myCursorAdapter);

Then result would be along the lines of :-

enter image description here

Changing to use a different stock layout and 2 fields as per :-

    gridview = this.findViewById(R.id.gridView1);
    csr = DBHelper.getAllRows();
    myCursorAdapter = new SimpleCursorAdapter(
            getBaseContext(),
            android.R.layout.simple_list_item_2,
            //android.R.layout.activity_list_item, //<<<< Changed Layout
            csr,
            new String[]{
                    SO50674769DBHelper.COURSES_KEY_FIELD_ID1,
                    SO50674769DBHelper.FIELD_MATERIALDESC,
                    //SO50674769DBHelper.FIELD_MATERIALNUM
            },
            new int[]{android.R.id.text1, android.R.id.text2}, //<<<< ID's of the 2 views
            0
    );
    gridview.setAdapter(myCursorAdapter);
  • Note The DatabaseHelper class is so named for my convenience.

Would result in :-

enter image description here

As such I suspect that you need to change the layout to a one of your own.

Additionally, as hinted at your getAllRows method is not at all ideal.

  • Checking for a null Cursor is useless as a Cursor returned from rawQuery will not be null. It may be empty in which case the Cursor getCount method would return 0 ().
  • moveToFirst is also a waste.

Simply have :-

public Cursor getAllRows() {
  SQLiteDatabase db = this.getReadableDatabase();
  return db.rawQuery("SELECT  * FROM " + DATAALL_TABLE, null);
}

Upvotes: 2

Related Questions