0

i need to join two listviews in one and add two blank fields beetween the results of the first and the second part.

Since i am using a cursor i have used "select ... union select ..." and joined the results, however i am having trouble inserting the empty rows in the middle since i give them negative ids in order to identify them later and they get at the top or at the bottom of the list, but i need them somehow in the middle beetween the two data sets.

Also i need to know which are the empty fields so that i can disable them, i have also tried MergeCursor but it duplicates my results.

max4ever
  • 11,909
  • 13
  • 77
  • 115

2 Answers2

1

If you use CommonWare's MergeAdapter it should be quite simple to achieve your desired result.

It will take multiple lists (and you can add individual views as well, so your empty lines can be inserted) and put them all together to be used in your list.

Here is a link to an answer I recently gave giving a more in depth explanation of how to use the MergeAdapter.

EDIT

It sounds like your query with the UNION and JOIN is giving you duplicate results in the cursor. Try separating them into two separate cursors. Then create two list adapters, and your two empty views, and add that all into the mergeadapter and set it to your listview.

lv = (ListView) v.findViewById(android.R.id.mergelist);

Cursor c1 = db.fetchCursor1();
String[] from1 = new String[] { DB.ITEM_1 };
int[] to1 = new int[] { R.id.ListItem1 };
SimpleCursorAdapter ca1 = new SimpleCursorAdapter(getActivity(),
    R.layout.your_layout, c1, from1, to1);

Cursor c2 = db.fetchCursor2();
String[] from2 = new String[] { DB.ITEM_1 };
int[] to2 = new int[] { R.id.ListItem1 };
SimpleCursorAdapter ca2 = new SimpleCursorAdapter(getActivity(),
    R.layout.your_layout, c2, from2, to2);

View Blank1 = getLayoutInflater.inflate(R.layout.blank);
View Blank2 = getLayoutInflater.inflate(R.layout.blank);

myMergeAdapter = new MergeAdapter();
myMergeAdapter.addAdapter(ca1);
myMergeAdapter.addView(Blank1);
myMergeAdapter.addView(Blank2);
myMergeAdapter.addAdapter(ca2);

lv.setAdapter(myMergeAdapter);
Community
  • 1
  • 1
Barak
  • 16,318
  • 9
  • 52
  • 84
  • 1
    What do you mean duplicates results? Maybe post the code of how you're trying to use it so we can see what's going on. – Barak May 14 '12 at 13:39
  • 1
    Please edit your question and post the code you are using there, as it shouldn't be giving you duplicates. It would be useful to see the query and how you are setting up the listview. – Barak May 14 '12 at 13:48
0

I think you can use a simple trick. Add your rows normally with empty rows with positive ids. Maintain the id of empty rows and you can disable them.

ArrayList <int> empty_row_id = new ArrayList<int>();


if (rows is empty) {
     empty_row_id.Add(row_id);
}

whenever you fill that row then just search for id and remove from empty_row

Hope it will help you..

Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29
  • i can't use positive ids since positive ids are used in the table and i risk overwriting another row's id – max4ever May 14 '12 at 12:40