0

I want to populate my Gridview with entries from my SQLite database but it does not work.

In my main activity I set a adapter to the gridview:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.room_layout);

    gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new RoomImageAdapter(this));
    Button addRoom = (Button) findViewById(R.id.addRoom);
    //OnClickListener for room button
    addRoom.setOnClickListener(new Button.OnClickListener() {            
        public void onClick(View view) {  
          addDeviceOrRoom();
        }  
    });
}

The class RoomImageAdapter looks like this:

    public class RoomImageAdapter extends BaseAdapter {
        private Context mContext;
        private static SQLiteDatabaseAdapter dbHelper;
        private static int i=0;

        public RoomImageAdapter(Context c) {
            mContext= c;
        }

        @Override
        public int getCount() {
            // defines how many items in the gridview are shown
            return 7;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v;
            readDatabase();
            if(convertView==null){
                LayoutInflater li = LayoutInflater.from(mContext);
                v = li.inflate(R.layout.imageandtextforgrid, null);
                TextView tv = (TextView)v.findViewById(R.id.icon_text);
                tv.setText("Raum: "+position);
                ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
                iv.setImageResource(R.drawable.livingroom);
            }
            else
            {
            v = convertView;
            }
            return v;
        }

        public void readDatabase(){ 
            Log.d("DB", "Start DB read");
            String name;
            dbHelper = new SQLiteDatabaseAdapter(mContext);
            dbHelper.open();

            Cursor cursor = dbHelper.getRoomName();
            cursor.moveToFirst();
            if(! cursor.isAfterLast()) {
                do {
                    name= cursor.getString(1);
                    Log.d("DB", "RoomName is: "+name);
                } while (cursor.moveToNext());
           }
           cursor.close();
           dbHelper.close();
           Log.d("DB", "Stop DB read");
        }
    }

And the code in my SQLite database is:

/**
  * GetRoomName
  */
 public Cursor getRoomName() {

      Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
        null, null, null, null, null, null}, 
        null, null, null, null, KEY_ROOMNAME);

      if (mCursor != null) {
       mCursor.moveToFirst();
      }
      return mCursor;
     }

But the line name= cursor.getString(1);inside the RoomImageAdapter is never executed and the cursor seems to be null. Anyone an idea?

Mokkapps
  • 2,028
  • 9
  • 42
  • 67

1 Answers1

0

Because you have not added any items into your Adapter.

Also, please do not call readDatabase() inside getView(), it will make loading very slow. Inside readDatabase(), you can add items into your Adapter and then notify adapter.

Munish Katoch
  • 517
  • 3
  • 6