-2

I am trying to access all the data from database "listOfFolder" table "folder" and want to store the data in a string array folders[] but i am getting how to resolve ArrayIndexOutOfBoundsException.

try{

  mydb = openOrCreateDatabase("listOfFolder", MODE_WORLD_WRITEABLE, null);  

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
      }

int count = 0;
String folders[] = null;
Cursor folderCursor = mydb.query("folder", null, null, null, null, null, null); 
while(folderCursor.moveToNext()) {

  folders[count] = folderCursor.getString(folderCursor.getColumnIndex("name"));
                    count++;
}

ListView list = (ListView)findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(DropboxActivity.this, android.R.layout.simple_list_item_1,folders);
list.setAdapter(adapter);
setContentView(R.layout.listoffolder);
0gravity
  • 2,682
  • 4
  • 24
  • 33
sachin
  • 45
  • 1
  • 2
  • 9

3 Answers3

4

Here's your problem:

String folders[] = null;

What did you expect to happen?

This code is a bad idea, because you have no idea how large a set the query will bring back. You have to call new to allocate a large enough array.

I'd prefer a collection like a List if it's available to you. Check this one out, too.

duffymo
  • 305,152
  • 44
  • 369
  • 561
4

Change

String folders[] = null;
Cursor folderCursor = mydb.query("folder", null, null, null, null, null, null); 
while(folderCursor.moveToNext()){
    folders[count] = folderCursor.getString(folderCursor.getColumnIndex("name"));
    count++;
}

to

Cursor folderCursor = mydb.query("folder", null, null, null, null, null, null);
if (folderCursor.moveToFirst()) {
    String folders[] = new String[folderCursor.getCount()];
    do {
        folders[count] = folderCursor.getString(folderCursor.getColumnIndex("name"));
        count++;
    } while(folderCursor.moveToNext());
}
vasart
  • 6,692
  • 38
  • 39
  • after that i am trying to create a listview and again i am getting nullpointerexception there ListView list = (ListView)findViewById(R.id.lv); ArrayAdapter adapter = new ArrayAdapter(DropboxActivity.this, android.R.layout.simple_list_item_1,folders); list.setAdapter(adapter); setContentView(R.layout.listoffolder); – sachin Jul 03 '12 at 20:37
  • getting null pointer exception at list.setAdapter(adapter); – sachin Jul 03 '12 at 20:39
  • Possibly `findViewById` can't find your ListView. Are you sure there is a ListView with id `@+id/lv` in your layout xml file? – vasart Jul 03 '12 at 20:42
  • yes i have a layout name listoffolder and i have use a list view there with id lv – sachin Jul 03 '12 at 20:46
  • i got where the error was i have to use setContentView(R.layout.listoffolder); before ListView list = (ListView)findViewById(R.id.lv); – sachin Jul 03 '12 at 20:53
0

Like this:

ArrayList<datatypehere> namehere=new ArrayList<datatypehere>();

Then you can add elements to the list by:

String example1 = "this is a string";
namehere.add(example1);

Theres quite abit on lists in the JavaDocs and Java Tutorials if you search for them.

Darryl Bayliss
  • 2,677
  • 2
  • 19
  • 28