1

This is the class where I try to add elements from cursor to ArrayList.

public ArrayList<String> getArrayList()
        {
            int i;
            sdb=this.getReadableDatabase();
            c=null;
            ArrayList<String> list=null;
            try{
            c=sdb.rawQuery("select * from Products", null);
            c.moveToFirst();
            if(c!=null)
            {
                list=new ArrayList<String>();
                do
                {
                    for(i=1;i<=c.getColumnCount();i++)
                    {
                        list.add(c.getString(c.getColumnIndex("ProductName")));
                        list.add(c.getString(c.getColumnIndex("ProduceType")));
                        list.add(c.getString(c.getColumnIndex("Company")));
                        list.add(c.getString(c.getColumnIndex("Price")));
                        list.add(c.getString(c.getColumnIndex("Quantity")));
                    }
                }while(c.moveToNext());     
            }
            else
                System.out.println("c null");
            }catch(Exception e)
            {
                e.printStackTrace();
                //c=null;
            }
            /*finally{
                if(c!=null && !c.isClosed())
                {
                    c.close();
                    c=null;                 
                }
                close();
            }*/
            return list;
        }

This is where I try to retrieve data from ArrayList and add them to expandablelistview.I want the 1st column of every row in cursor in the group view and the remaining columns of the corresponding row in child view.

private void loadData(){

                db=new DatabaseHelper(this);
                //db.open();
                System.out.println("returned from db.open() in loadData");
                ArrayList<String> al=db.getArrayList();
                db.close();
                int count=al.size();
                int i,j;
                try{
                for(i=1;i<=count;i=i+5)
                        {
                            String pn=al.get(i);
                            for(j=i;j<=i+5;j++)
                            {
                                String inf=al.get(j);
                                addProduct(pn,inf);
                            }
                        }
                }catch(StackOverflowError e)
                {
                    e.printStackTrace();
                }

         }

I am going wrong with the for loop as of my knowledge.Logcat is showing the following error

07-04 02:44:14.747: E/CursorWindow(1881): Failed to read row 0, column -1 from a CursorWindow which has 3 rows, 5 columns.

Please tell me the correct usage of arraylist data retrieval.Thanks in advance.

3
  • Seems like a lot of code. Commented Jul 4, 2014 at 6:52
  • Wouldn't it be a better approach to keep an ArrayList of object of a class which include all this strings? Commented Jul 4, 2014 at 6:53
  • removes this for loop for(i=1;i<=c.getColumnCount();i++) which results in adding same data Commented Jul 4, 2014 at 6:55

2 Answers 2

2

I would suggest going with this approach, as the approach you are using is a bad practice.

Instead of having List of String, use an object of a custom class called ProductInfo, (which include all these Strings), and add and retrieve data from that List.

public class ProductInfo{

    String productName, productType, company, price, quantity;
    //getter setter

}

// add data
    List<ProductInfo> productList = new ArrayList<ProductInfo>();
    Cursor c = null;
    do {
        for (int i = 1; i <= c.getColumnCount(); i++) {

            ProductInfo pInfo = new ProductInfo();

            pInfo.setProductName(c.getString(c
                    .getColumnIndex("ProductName")));
            pInfo.setProductType(c.getString(c
                    .getColumnIndex("ProduceType")));
            pInfo.setCompany(c.getString(c.getColumnIndex("Company")));
            pInfo.setPrice(c.getString(c.getColumnIndex("Price")));
            pInfo.setQuantity(c.getString(c.getColumnIndex("Quantity")));

            productList.add(pInfo);
        }
    } while (c.moveToNext());

// retrieve data

    for (int i = 0; i < productList.size(); i++) {

        System.out.println("Info of Product "+(i+1));
        ProductInfo pInfo = productList.get(i);
        System.out.println("Prod name: "+ pInfo.getProductName());
        System.out.println("Prod Type: "+pInfo.getProductType());
        System.out.println("Company: "+pInfo.getCompany());
        System.out.println("Price: "+pInfo.getPrice());
        System.out.println("Quantity: "+pInfo.getQuantity());

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

Comments

0

Try this way,hope this will help you to solve your problem.

    public ArrayList<HashMap<String, String>> getArrayList() {
        int i;
        sdb = this.getReadableDatabase();
        c = null;
        ArrayList<HashMap<String, String>> list = null;
        try {
            c = sdb.rawQuery("select * from Products", null);
            c.moveToFirst();
            if (c != null) {
                list = new ArrayList<HashMap<String, String>>();
                do {
                    for (i = 1; i <= c.getColumnCount(); i++) {
                        HashMap<String, String> row = new HashMap<String, String>();
                        row.put("ProductName", c.getString(c.getColumnIndex("ProductName")));
                        row.put("ProduceType", c.getString(c.getColumnIndex("ProduceType")));
                        row.put("Company", c.getString(c.getColumnIndex("Company")));
                        row.put("Price", c.getString(c.getColumnIndex("Price")));
                        row.put("Quantity", c.getString(c.getColumnIndex("Quantity")));
                        list.add(row);
                    }
                } while (c.moveToNext());
            } else
                System.out.println("c null");
        } catch (Exception e) {
            e.printStackTrace();
            //c=null;
        }
            /*finally{
                if(c!=null && !c.isClosed())
                {
                    c.close();
                    c=null;
                }
                close();
            }*/
        return list;
    }

    private void loadData() {

        db = new DatabaseHelper(this);
        //db.open();
        System.out.println("returned from db.open() in loadData");
        ArrayList<HashMap<String, String>> al = db.getArrayList();
        db.close();
        int count = al.size();
        int i, j;
        try {
            for (HashMap<String, String> product : al) {
                System.out.println("ProductName: " + product.get("ProductName"));
                System.out.println("ProduceType: " + product.get("ProduceType"));
                System.out.println("Company: " + product.get("Company"));
                System.out.println("Price: " + product.get("Price"));
                System.out.println("Quantity: " + product.get("Quantity"));
            }
        } catch (StackOverflowError e) {
            e.printStackTrace();
        }

    }

1 Comment

I hv made changes as suggested by you.I think there is something wrong with my for loop iteration in getArrayList() method.Still the same error is persisting. @Haresh

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.