0

I have a simple sqlite function which returns everything from my table.Currently I am getting single values I would like to get an array of values.This is the code I am trying to implement :-

   public void getItemDetails()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery("SELECT * FROM "+ORDER_TABLE, null);
        if(c.moveToFirst()){
            do{

                //assing values
                int orderId = c.getInt(0);
                String serviceName = c.getString(1);
                String categoryName = c.getString(2);
                String itemName = c.getString(3);
                int itemRate = c.getInt(4);
                int itemQty = c.getInt(5);
                int itemTot = c.getInt(6);
                //Do something Here with values


                Log.d("Order Table ",orderId+" , "+serviceName+" , "+categoryName+" , "+itemName+" , "+itemRate+" , "+itemQty+" , "+itemTot);

            }while(c.moveToNext());
        }
        c.close();
        db.close();

    }

What changes do I have to make to my existing code to get an array of orderId,serviceName,categoryName,itemName,itemRate,itemQty and itemTot.

Any help or suggestion is appreciated.Thank you.

4 Answers 4

2

Follow these steps.

  1. First create a bean(getter setter class) like..

    public class ItemDetails{
    
    int orderId;
    // declare variable for all data here and also getter setter method(like below for orderid)
    
    // Empty constructor
    public ItemDetails(){
    
    }
    
    public int get_orderId() {
    return orderId;
    }
    
    public void set_orderId(int orderId ) {
    this.orderId = orderId;
    }
    }
    
  2. Now use this method..

    // Getting All data
    public List<ItemDetails> getAllItemDetails() {
    List<ItemDetails> listAll = new ArrayList<ItemDetails>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + ORDER_TABLE;
    
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
    
            ItemDetails operatorTable = new ItemDetails();
            //here get all data from cursor and set it into setter method like below
           operatorTable.set_orderId(Integer.parseInt(cursor.getString(0)));
            //operatorTable.set_Operator(cursor.getString(1));
    
            operatorList.add(operatorTable);
        } while (cursor.moveToNext());
    }
    
    // returnlist
    return listAll ;
    

    }

  3. call this mehod like..

    List<ItemDetails > itemList = db.getAllItemDetails();
    

Hope this will help you.

Sign up to request clarification or add additional context in comments.

1 Comment

Good suggestion of collecting the information in a list of POJOs than in separate arrays of primitive values. An ORM like JDXA can further simplify the implementation by eliminating the need to write low level code involving SQL statements and cursor processing.
0

You can create Array of serielizable object

Itemdetail i1=new Itemdetail();

Then Create properties and its setter getter methods Initialize that object in your getItemDetails function set your values in setter method of object and return object from function

You can do like this

public Itemdetail getItemDetails(){

Itemdetail i1=new Itemdetail();
i1.setServiceName("Your value");
...
.

 return i1;
}

Comments

0

You can use ArrayList for that:-

List<String> list=new ArrayList<>();
list.add(String.valueOf(orderId));
list.add(serviceName);
list.add(categoryName);
list.add(itemName);
list.add(String.valueOf(itemRate));
list.add(String.valueOf(itemQty));
list.add(String.valueOf(itemTot));

instead of void return this list in your getItemDetails(), You can also use model class for setting the value in list

Comments

0

Id change the way you're accessing items from your table to make it clearer.

String serviceName = cursor.getString(aCursor.getColumnIndex("column_name"));

Second, I'd have each row as an Object Item

Then as @Nainal has suggested I'd iterate through the DB, populating each Object and adding to the 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.