0

I'm trying to retrieve a column (a database column where names are saved) and puting them to a listview. I have a class called Data with "getters" and "setters".

The following code is placed in a DBHandler class which extends SQLiteOpenHelper. This code is called from the MainActivity.java where the listview is meant to be updated with an onClickButton event.

public String[] getNames (int a, int b) { 

        String[] names = new String[] {};

        String selectQuery = "SELECT * FROM " + TABLE_NAME 
                + " WHERE " + KEY_ONE + " = ? AND " + KEY_TWO + " = ?";


        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, new String[]{String.valueOf(a), String.valueOf(b)});

        if (cursor.moveToFirst()) {

            int i = 0;

            do {
                Data myData = new Data();

                names [i] = cursor.getString(1); //Names in cursor

                ++i;

            } while (cursor.moveToNext());

        }
        return names;
    }

In the MainActivity.java I call the following code before updating and notifying the update of the listview adapter:

values = db.getNames (1, 1);

I don't know why but this isn't working, it throws many errors with String lengths and crashes the app when I click the button that is suposed to enter the onClickButton.

Thanks

1
  • Please upload those error logs Commented Apr 11, 2014 at 8:07

3 Answers 3

1

Follow the laalto answer and at last convert your ArrayList to Array like below:

String[] arrRecords = names.toArray(new String[names.size()]);
Sign up to request clarification or add additional context in comments.

Comments

0
String[] names = new String[] {};

...

names [i] = cursor.getString(1); //Names in cursor

You're assigning to an empty array which causes ArrayIndexOutOfBoundsException.

Consider using a list such as ArrayList where you can append your values, e.g.

List<String> names = new ArrayList<String>();

...

names.add(cursor.getString(1));

If you really need to return a String[], you can convert the list with toArray():

String[] arr = new String[names.size()];
names.toArray(arr);
return arr;

Also, when posting questions that involve exceptions, always include the exception stacktrace from logcat in the question itself.

3 Comments

How can I change that ArrayList to a String[] which is what I really need after all?
There's toArray(T[] array)
Is the other way, I need a String[] and I have an ArrayList
0

Why use like this

 String[] names = new String[] {}; //no size

 names [i] = cursor.getString(1); //it can work?

Use ArrayList<String>

List<String> names = new ArrayList<String>(); //declare

names.add(<column-val>);  //add column value to list

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.