1

I have an SQLite Database.

Here's some of the code setting it up:

// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_DESCRIPTION = "description";

public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_NAME, KEY_DESCRIPTION};

// Column Numbers for each Field Name:
public static final int COL_ROWID = 0;
public static final int COL_NAME = 1;
public static final int COL_DESCRIPTION = 2;

// DataBase info:
public static final String DATABASE_NAME = "dbMetrics";
public static final String DATABASE_TABLE = "mainMetrics";
public static final int DATABASE_VERSION = 1; 

//SQL statement to create database
private static final String DATABASE_CREATE_SQL =
        "CREATE TABLE " + DATABASE_TABLE
                + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + KEY_NAME + " TEXT NOT NULL, "
                + KEY_DESCRIPTION + " TEXT"
                + ");";

I want to output one of my columns KEY_NAME as an ArrayList.

To do this, I have so far generated the following code:

public ArrayList<String> getAllStringValues() {
        ArrayList<String> myStringValues = new ArrayList<String>();
        Cursor result = db.query(true, DATABASE_TABLE,
                new String[] {KEY_NAME}, null, null, null, null,
                null, null);

            **********CODE NEEDED HERE***********
        

        System.out.println(Arrays.toString(myStringValues.toArray()));
        return myStringValues ;

    }

However, I am not sure how I get the data from the column into an ArrayList from here.

My Question

Can someone give me some assistance as to how to design a loop that will go through each row in the column and put that data in an ArrayList?

I'm pretty sure I will need to at some point use

result.getString(result.getColumnIndex(KEY_NAME))

in order to do this, but again, I'm not sure how.

Any help would be greatly appreciated.

Community
  • 1
  • 1
Mr.NoName
  • 125
  • 4
  • 13
  • Read the documentation for the Cursor class? There are a few methods that stick out to iterate it – OneCricketeer Oct 06 '16 at 07:45
  • 1
    Start with this, then learn how to extract data out of the Cursor. http://stackoverflow.com/questions/10723770/whats-the-best-way-to-iterate-an-android-cursor – OneCricketeer Oct 06 '16 at 07:46
  • 1
    See https://stackoverflow.com/questions/2810615/how-to-retrieve-data-from-cursor-class – Kirill Oct 06 '16 at 07:47
  • The problem is, the answers provided here seem to result in only unique values being printed. Like for instance, I want my output to print `[Test,Test2,Test2,Test]` but instead it removes duplicates and prints `[Test,Test2]`. How can this be avoided? I want it to print everything, not merely distinct ones. – Mr.NoName Oct 06 '16 at 08:03

1 Answers1

0
 if (Cursor.moveToFirst()) {
                do {
                    keyname.add(Cursor.getString(Cursor.getColumnIndex("keyname")));
                    key_description.add(Cursor.getString(Cursor.getColumnIndex("key_description")));

                } while (Cursor.moveToNext());
            }
            Cursor.close();

try this code if doesnt work, tell me

Mayank Bhatnagar
  • 1,316
  • 2
  • 13
  • 21
  • Here's the problem: it works, but some of the entries have the same value. Like for instance, it should print `[Test,Test2,Test2,Test]` but instead it removes duplicates and prints `[Test,Test2]`. How can this be avoided? I want it to print everything, not merely distinct ones. – Mr.NoName Oct 06 '16 at 08:00
  • no it will not remove duplicates, arraylist can hold duplicate enteries. I have tried this code and it works fine. – Mayank Bhatnagar Oct 06 '16 at 17:27
  • It does for me. – Mr.NoName Oct 06 '16 at 18:49
  • can you print these arraylist in logcat and show to me.. as well as print all table in logcat. Then we can see whats going wrong in your case. – Mayank Bhatnagar Oct 07 '16 at 02:59
  • I resolved it. Amazingly, the word "true" in the `db.query` part was the problem and was removing all the non-distinct entries. No idea why. I just did a series of debugging steps and figured it out after several hours. – Mr.NoName Oct 07 '16 at 06:13
  • I have a separate question here: http://stackoverflow.com/questions/39906892/how-can-i-use-viewbinder-to-connect-switches-to-my-sqlite-database if you have a chance to offer ur words of wisdom it would be appreciated :) – Mr.NoName Oct 07 '16 at 06:14