0

I have a SQLite database that contains multiple tables. For each table, I am trying to represent all the data in an ExpandableListView using a custom CursorTreeAdapter. As far as I understand, the method getChildrenCursor returns a cursor that points to the data I needed to populate my child views. However, I do not have a concrete idea on how to retrieve the children cursor using the groupCursor parameter.

@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
    String[] projection = { "columnNeeded" };
    return context.getContentResolver()
            .query(CONTENT_URI, projection, null, null, null);
}

the above method will return a cursor that returns all rows containing the column I need. Is this the right way to do this?

In the "columnNeeded" column for each row of the table, it contains a String representation of a jsonArray. I was trying to store an arrayList into each row of the table using JSONArray. Therefore, I am trying to retrieve this arrayList and populate the child views with this arrayList like so:

@Override
protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) {
    TextView name = (TextView) view.findViewById(R.id.summary_child_name);
    TextView bill = (TextView) view.findViewById(R.id.summary_child_bill);

    String arrayListString = cursor
            .getString(cursor.getColumnIndex("columnNeeded"));
    JSONObject json = new JSONObject();
    try {
        json = new JSONObject(arrayListString);
    } catch (JSONException e) {
        Log.e(LOG_TAG, "Unable to retrieve list of items");
    }
    JSONArray jsonArray = json.optJSONArray(ReceiptContract.JSONARRAY_NAME);
    // retrieveArrayList iterates through jsonArray and adds it to items 
    items = retrieveArrayList(jsonArray);

    name.setText(What do I put here?);
    bill.setText(What do I put here?);
}

As you can see I have managed to retrieve the entire array list as an ArrayList type object. However, I am stuck on displaying the array list in the child views. Any idea on how I can go about doing this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
L. KT
  • 37
  • 1
  • 9
  • you are storing json string in your sqlite table?? what for? – pskink Jul 12 '17 at 07:45
  • @pskink I was looking for ways to store the entire ArrayList in each row of table instead of creating another table to be referenced. Hence, I came across this suggestion: https://stackoverflow.com/questions/5703330/saving-arraylists-in-sqlite-databases do you know if there is a better way to achieve the same thing? – L. KT Jul 12 '17 at 08:56
  • whats inside that json array? just some strings? whats the size of it? is is always the same size? – pskink Jul 12 '17 at 09:02
  • @pskink The jsonArray contains String and double pairs. I populated the jsonArray with the following: JSONArray jsonArray = new JSONArray(itemsList) where itemsList is an ArrayList that holds objects with the custom datatype, Item. Each Item object holds a String and a double value. The size depends on the user input in earlier activities. However, I still would like the users to be able to modify the data in the database – L. KT Jul 12 '17 at 09:25

1 Answers1

0

Use HashMap to store your data as key value and populate your data into your ExpandableListView using BaseExpandableListAdapter

Utkarsh Joshi
  • 111
  • 1
  • 6