2

I retrieve all the rows from my SQLite-DB Table

Cursor  cursor = db.rawQuery("select * from table",null);

Then I want to add each row to an ArrayList

Please help!

3 Answers 3

2

IF you're using custom object array, then use like following :

public void fillDataToArray() {

            ArrayList<BeanClass> arrayList = new ArrayList<BeanClass>();
            DatabaseHandler db = new DatabaseHandler(getActivity());
            db.open();
            try
        {
            Cursor c = db.getImageNameForList();

                if (c != null && c.getCount() > 0) {
                c.moveToFirst();
                for (int count = 0; count < c.getCount(); count++) {
                     Beanclass detail = new Beanclass ();

                     detail.setName(c.getString(c
                     .getColumnIndex(DatabaseHandler._ID)));

                     detail.setPath(c.getString(c
                     .getColumnIndexOrThrow(DatabaseHandler._PATH)));

                     arrayList.add(detail);
                    c.moveToNext();

                    }
                }
        }
    catch(Exception e)
        {
        e.printStackTrace();
        }
            c.close();
            db.close();

        }

If you use this in activity, change getActivity() to local context or whichever way you use context in activities.

DATA RETRIEVAL METHOD IN DB ADAPTER CLASS :

> public Cursor getImageNameForList() {         
return db.rawQuery("select " + IMAGE_PATH + " from "+ TABLE_PRODUCT_IMAGES , null);     
}
Sign up to request clarification or add additional context in comments.

2 Comments

If I want to display "Image ID" of 2nd item in ArrayList<BeanClass> in a TextView how would I do that please? I can create the xml layout I just want the JAVA part.
textView.setText(arrayList.get(1).getImageID().toString()); Here "1" is the position/index of the second item of arraylist.
2

Code to do that would look like this, depending on whats in your Table. You have to create a Class that is able to represent a Row in your table.

For example if there are people in your table, with name and age, you will need an object Human(String name, int age) to put into a list.

SQLiteDatabase db = helper.getReadableDatabase();
ArrayList<Human> results = new ArrayList<Human>();

try {

    String rawQuery = "Select name, age from people";

    Cursor c = db.rawQuery(rawQuery, null);

    try {

        if (!c.moveToFirst())
            return null;

        do {
            results.add(new Human(c.getString(0),c.getInt(1)));
        } while (c.moveToNext());
    } finally {
        c.close();
    }
} finally {
    db.close();
}



public class Human {
    private String name;
    private int age;

    public Human (String name, int age) {
        this.name = name;
        this.age = age;
    }
}

3 Comments

Can you elaborate more on Human, Name, Age example please, and adapt it to the solution above.
@eMM done, udated the code and provided the Human class as well as a proper sql query
Creative solution Daniel. +1
0

In your Db Adapter class, add this method

public ArrayList<String>getData()
{
    ArrayList<String>names=new ArrayList<String>();
    Cursor c = getReadableDatabase().rawQuery("Select * from tableName", null);
    c.moveToFirst();
    do
    {
        String s1=c.getString(c.getColumnIndex("<Column_Name>"));
        names.add(s1);
    }while(c.moveToNext());     
    return names;
}

after that in your activity class add this

ArrayList<String> cid;
DB_Adapter database = new DB_Adapter(getApplicationContext(), "<your schema name>", null, 1);
cid=database.getData();

After that add your arraylist to your array adapter which is set to ListView

2 Comments

What is "<your schema name>" please?
it is the name of your database, you can give any name you want.

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.