Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read from a database in android to an arraylist

I'm very new to android development and can't figure out how to read from my database/table into an arrayList. Right now I have:

      Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);

       int Column1 = c.getColumnIndex("Field1");
       int Column2 = c.getColumnIndex("Field2");

       // Check if our result was valid.
       c.moveToFirst();
       if (c != null) {
        // Loop through all Results
        do {
         String Name = c.getString(Column1);
         int Age = c.getInt(Column2);
         Data =Data +Name+"/"+Age+"\n";
        }while(c.moveToNext());
       }
       TextView tv = new TextView(this);
       tv.setText(Data);
       setContentView(tv);
      }
      catch(Exception e) {
       Log.e("Error", "Error", e);
      } finally {
       if (myDB != null)
        myDB.close();
      }

How do I read this into an arraylist?

like image 236
Jason Avatar asked Dec 16 '22 11:12

Jason


2 Answers

Here is a code which shows you how to get ArrayList from Database query.

public ArrayList<String> GetAllValues(String aTable,String[] aColumn)
{
    ArrayList<String> list = new ArrayList<String>();
    Cursor cursor = sqlDB.query(aTable, aColumn, null, null, null, null, null);
    if (cursor.moveToFirst())
    {
        do
        {
            list.add(cursor.getString(0));
        }
        while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) 
    {
        cursor.close();
    }

    return list;
}

If you provide null in "aColumn", it will give you all columns. I hope it will help.

like image 95
Khawar Avatar answered Jan 03 '23 20:01

Khawar


//ArrayList of Calllog class objects
ArrayList<CallLog> arrCallLogs = null;
Cursor curCalllog = myDB.rawQuery("SELECT * FROM " + TableName , null);

            if(curCalllog != null && curCalllog.moveToFirst()) {
            arrCallLogs = new ArrayList<CallLog>();
            while (curCalllog.isAfterLast() == false) {
                //Calllog is a class with list of fileds 
                CallLog callLog = new CallLog();
                callLog.setId(curCalllog.getLong(curCalllog.getColumnIndex(KEY_ROWID)));
                callLog.setName(curCalllog.getString(curCalllog.getColumnIndex(KEY_NAME)));
                callLog.setNumber(curCalllog.getString(curCalllog.getColumnIndex(KEY_NUMBER)));
                callLog.setNumberType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NUMBERTYPE)));
                callLog.setCallType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_CALLTYPE)));
                callLog.setDatetime(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DATETIME)));
                callLog.setDuration(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DURATION)));
                callLog.set_new(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NEW)));
                arrCallLogs.add(callLog);
                curCalllog.moveToNext();
            }
        }
        curCalllog.close();
    myDB.close();

This is the example how you can make a arrayList from Cursor.

Here there is a class name Calllog which has list of fileds and setter and getter methods. Here i had make a ArrayList of Calllog objects using Cursor

like image 40
Dharmendra Avatar answered Jan 03 '23 21:01

Dharmendra