title: Populate GridView in Android with SQLite database entries
body: <p>I want to populate my Gridview with entries from my SQLite database but it does not work.</p>
  
  <p>In my main activity I set a adapter to the gridview:</p>
  
  <pre><code>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();
          }  
      });
  }
  </code></pre>
  
  <p>The class RoomImageAdapter looks like this:</p>
  
  <pre><code>    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");
          }
      }
  </code></pre>
  
  <p>And the code in my SQLite database is:</p>
  
  <pre><code>/**
    * 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;
       }
  </code></pre>
  
  <p>But the line <code>name= cursor.getString(1);</code>inside the RoomImageAdapter is never executed and the cursor seems to be null. Anyone an idea?</p>