1

Im making an android app and this part is where a cursor will go through a database and store the 'title' section of the table into a string array. This is then called in another class and is used to dynamically show buttons based on the entries. The code for putting the titles into an array is as follows:

public String[] getTitles()
{
    SQLiteDatabase db =getReadableDatabase();
    int numRows = (int) DatabaseUtils.queryNumEntries(db, SPORTS_TABLE_NAME);
    String title;
    String[] titleArray = new String[100];
    String sql = "SELECT Title FROM Sports;";
    Cursor cursor = db.rawQuery(sql, null);
    int i = 1;

    while (cursor.moveToNext()) {

        if(cursor != null && cursor.moveToFirst()) {
            title = cursor.getString(0);
            titleArray[i] = title;
            i++;
        }
        if(cursor != null)
            db.close();

    }
    cursor.close();
    return titleArray;
}

Then it is called with the following code:

int i = 1;

    String[] titlesArray = db.getTitles();
    for(String titles: titlesArray){
        Button btn = new Button(this);
        btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        btn.setId(i);
        btn.setText(titlesArray[i]);
        ll.addView(btn);
        i++;
    }

Been looking for a while and think it needs a fresh pair of eyes.. any ideas?

4
  • 1
    Post your logcat errors. Commented Nov 12, 2014 at 11:53
  • 1
    What I saw at once: if(cursor != null && cursor.moveToFirst()) this will move the cursor to first entry in each iteration of the loop ... and: what if there are more than 100 titles? Commented Nov 12, 2014 at 11:56
  • java.lang.ArrayIndexOutOfBoundsException at com.example.dbHelper.getTitles(dbHelper.java:171) --- That is the titleArray[i] = title; line Commented Nov 12, 2014 at 11:56
  • 1
    btn.setText(titlesArray[i]); should it be btn.setText(titles); ? Commented Nov 12, 2014 at 11:57

1 Answer 1

4

what if your query returns more than 100 rows? If think it would be safer to use something like

String sql = "SELECT Title FROM Sports;";
Cursor cursor = db.rawQuery(sql, null);
if (cursor == null)
   return;
String[] titleArray = new String[cursor.getCount()];
 while (cursor.moveToNext()) {
     title = cursor.getString(0);
     titleArray[i] = title;
     i++;  
}

of, better, a Collection.

String sql = "SELECT Title FROM Sports;";
Cursor cursor = db.rawQuery(sql, null);
if (cursor == null)
      return;
ArrayList<String> titleArrayList = new ArrayList<String>();
while (cursor.moveToNext()) {
     title = cursor.getString(0);
     titleArrayList.add(title);       
}
Sign up to request clarification or add additional context in comments.

Comments

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.