2

I have made a database and arraylist android passing variables from inside database class , now I am trying to get one or some values from the array list using List<EntryEffectiveRate> allEffectiveRate = db.getAllEffectiveRates(); as for get the all the data lists from the database, however i only able to get all of them using loop :

 // getting all ER
                Log.d("Get ER", "GEtting All ER");

                List<EntryEffectiveRate> allEffectiveRate = db.getAllEffectiveRates();
                for (EntryEffectiveRate ER : allEffectiveRate) {
                    Log.d("ER_ids", String.valueOf(ER.getERId()));
                    Log.d("ER_rates", ER.getERKondisi());
                    Log.d("ER_tenors", String.valueOf(ER.getERTenor()));
                    Log.d("ER_rates", String.valueOf(ER.getERrate()));
                }

how to get just one or few values from the array list?

thanks in advance.

The solution is on the above link, check on the answer.

Community
  • 1
  • 1
Jedi Fighter
  • 377
  • 1
  • 4
  • 20

5 Answers5

4

i am posting a sample method that i just write in my project, definately it will be according to your requirement.

public ArrayList<SomeModelClass> GetCatagoriesName() {
    close();
    database = this.dbOpenHelper.getReadableDatabase();
    ArrayList<SomeModelClass> catagory_List = new ArrayList<SomeModelClass>();
    String selectQuery = " SELECT " + KEY_CATAGORIES + " FROM "
            + TABLE_CATAGORIES;
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                catagory_List.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
    }
    return catagory_List;
}

on the other side, just get it from arraylist with position 0, no need to iterate over for loop.

Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
3

Just create a new function that takes a parameter that you can pass to the where clause of your query.

Doing this, you won't have to do any filtering on the ArrayList that's returned from your DatabaseMaster class.

You can create a different function to filter on each database entry you need.

For example, to filter on a certain KEY_ER_USEDORNEW value, the new function would be something like this:

public List<EntryEffectiveRate> getSomeERates(String USEDORNEW) {
        List<EntryEffectiveRate> EffectiveRates = new ArrayList<EntryEffectiveRate>();
        String selectQuery = "SELECT  * FROM " + TABLE_EFFECTIVE_RATE + " where " + KEY_ER_USEDORNEW + " = ?";

        Log.e(LOG, selectQuery);

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, new String[] {USEDORNEW});

        // looping through all rows and adding to list
        if (c.moveToFirst()) {
            do {
                EntryEffectiveRate ergt = new EntryEffectiveRate();

                ergt.setERKondisi(c.getString(c
                        .getColumnIndex(KEY_ER_USEDORNEW)));              
                // add
                EffectiveRates.add(ergt);
            } while (c.moveToNext());
        }
        // db.close();
        c.close();
        return EffectiveRates;
    }

To call this from your Activity, you would pass the filter value as a parameter:

           String USEDORNEW = "Baru";
           List<EntryEffectiveRate> someEffectiveRates = db.getSomeERates(USEDORNEW);
            for (EntryEffectiveRate ER : someEffectiveRates) {           
                Log.d("ER_rates", ER.getERKondisi());
            }
Jedi Fighter
  • 377
  • 1
  • 4
  • 20
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
1

You can use ...

List.get(index);

method for getting the specific or one value from the list without looping it. But if you want more then one value then you have to loop it.

Khubab Hamza
  • 184
  • 12
1

Use the Following code to get just one value from arraylist

   List<EntryEffectiveRate> allEffectiveRate =  db.getAllEffectiveRates();
   EntryEffectiveRate ER =allEffectiveRate.get(position);
   Log.d("ER_ids", String.valueOf(ER.getERId()));
   Log.d("ER_rates", ER.getERKondisi());
   Log.d("ER_tenors", String.valueOf(ER.getERTenor()));
   Log.d("ER_rates", String.valueOf(ER.getERrate()));
Prashant Bhoir
  • 900
  • 6
  • 8
0

//use for loop as like below

for(int i=0;i<allEffectiveRate.size();i++)
  { 
     EntryEffectiveRate ER =allEffectiveRate.get(i);  
     Log.d("ER_ids"String.valueOf(ER.getERId()) ;
     Log.d("ER_rates", ER.getERKondisi());
     Log.d("ER_tenors", String.valueOf(ER.getERTenor()));
      Log.d("ER_rates", String.valueOf(ER.getERrate())); 

}


//if after that u can't get propar data at then problem with ur method //db.getAllEffectiveRates();