1

I am quite confused about how to combine two separate database queries into one listView.

Currently, my listView is populated by the following adapter which queries the damaged component table in my database and provides a list of damaged components for a certain location:

private class MyListAdapter extends ResourceCursorAdapter { 

    // In your ListActivity class, create a new inner class that extends ResourceCursorAdapter. 
    //This inner class is the custom CursorAdapter we will use to manage how data is bound to a list item:

    public MyListAdapter(Context context, Cursor cursor) { 
        super(context, R.layout.row_location, cursor);
    } 

    @Override
    public void bindView(View view, Context context, Cursor cursor) { 
        TextView text_first_line = (TextView) view.findViewById(R.id.location_row_item_main_text);
        TextView text_second_line = (TextView) view.findViewById(R.id.location_row_item_secondary_text);
        ImageView flagIcon = (ImageView) view.findViewById(R.id.flagIcon);

        String row_text_component = cursor.getString(cursor.getColumnIndex(RMDbAdapter.COMPONENT));
        String row_text_position = ", Position " + cursor.getString(cursor.getColumnIndex(RMDbAdapter.POSITION));
        if(row_text_position.equals(", Position Not Applicable")){
            row_text_position = "";
        }
        String row_text_action = " - " + cursor.getString(cursor.getColumnIndex(RMDbAdapter.ACTION_REQUIRED));

        text_first_line.setText(row_text_component + row_text_position + row_text_action);
        text_second_line.setText("Dexion Speedlock, S Duty, 3000mm");

        String risk = cursor.getString(cursor.getColumnIndex(RMDbAdapter.RISK));
        if (risk.equals("Red Risk")){
            flagIcon.setImageResource(R.drawable.red_flag);
        }
        else if (risk.equals("Green Risk")){
            flagIcon.setImageResource(R.drawable.green_flag);
        }
        else if (risk.equals("No Risk")){
            flagIcon.setImageResource(R.drawable.note);
        }

    }
}

This is triggered when I call the following when the activity is started:

private void setAdapter(){

    // Get a Cursor for the list items

    Cursor listComponentCursor = rmDbHelper.fetchDamagedComponentsForLocation(locationId);
    componentCursorSize = listComponentCursor.getCount();
    startManagingCursor(listComponentCursor); 
    // set the custom list adapter     
    setListAdapter(new MyListAdapter(this, listComponentCursor));

}

So I would also like to create a second query on a separate table (this time issues table) and add this to the listView under the list of damaged components.

Reading this Listview from multiple tables?, I believe that I should use a Join or Merge cursor. However, from what I have researched I still have no idea how to integrate either concept into my code.

Can anyone point me in the right direction please?

Community
  • 1
  • 1
Scamparelli
  • 756
  • 1
  • 12
  • 28

1 Answers1

2

Although you don't show it, I'm going to assume you overrode the newView method of the cursor adapter. A MergeCursor will just stack the results of one query on top of the next. A CursorJoiner will pretty much put the results side by side. It sounds like you want a MergeCursor.

If you want to just concatenate the results of a second query to the first, perform both queries and create a MergeCursor. If you need them to have different layouts, or they have different column names, you will need to override the getItemViewType method in the adapter. Then, in your newView method, you can inflate different views based on the type. In bindView you need to check the type and apply the correct values to the correct views.

A couple of random tips for your code:

1) use the ViewHolder pattern, it's way faster.

2) it's always better to do "literal string".equals(variable) instead of variable.equals("literal string") because a literal string cannot throw a NullPointerException

toadzky
  • 3,806
  • 1
  • 15
  • 26
  • Hi toadzky, thanks for the quick response and for the tips (will integrate these we I get a chance). What you see is all the code that fills the listView - I have not overrode the newView (not heard of this before). Can you elaborate on this element at all? Also I do have different column names in each of the tables, to do you have an example of how getItemViewType is overridden? Thanks for your assistance so far. – Scamparelli Oct 24 '12 at 21:11
  • there are 2 methods you have to override with a CursorAdapter: newView and bindView. newView inflates or creates the view. bindView assigns the values to the views. it looks like the ResourceCursorAdapter subclass takes care of that for you. do you have any specific reason to use that subclass? if not, just extend the CursorAdapter – toadzky Oct 24 '12 at 21:13
  • 1
    yeah. i posted a REALLY basic version of a cursor adapter for you to see [here](http://pastebin.com/KFwC4kqx) – toadzky Oct 24 '12 at 21:23
  • Sorry to be a pain, but a lot of what you have said is a bit over my head. I create my listComponentCursor in the setAdapter(), so is this where I use create second cursor and use MergeCursor? If so then the code in the bindView won't be relevant to the second set of data. Confused.com! – Scamparelli Oct 24 '12 at 21:25
  • after you make both queries. create a mergecursor (`new MergeCursor(new Cursor[] { a, b })`. pass that to your adapter – toadzky Oct 24 '12 at 21:28
  • Ok great, understand that. But that just leaves the issue of how bindView deals with the data. So, assuming I change from resourceCursorAdapter to cursorAdapter, am I correct in thinking getItemViewType means you can create two type of view and you then somehow test to see if the data has come from one table of the other before assigning the correct view? – Scamparelli Oct 24 '12 at 21:37
  • p.s. Thanks for your persistence. I can see this will all lead to another complication on how to get the data back out of the listView!! – Scamparelli Oct 24 '12 at 21:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18544/discussion-between-toadzky-and-scamparelli) – toadzky Oct 24 '12 at 21:40