I’m new to this ExpandableListView and I desperately need your help please.
I’m using the code below to create an ExpandableListView with SimpleCursorTreeAdapter because I’m geting my data from SQLite database.
It all works fine, but I can’t find a solution on how to add some items to the parent ListView (for now).
I’d like to add a category with a click on a TextView.
public class ListViewExp extends ExpandableListActivity {
private DBAdapter db;
private Cursor getMatchigItems;
private Cursor getAllCat;
private ExpandableListView expListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testexpview);
TextView tv1 = (TextView) findViewById(R.id.testtext);
db = new DBAdapter(this);
db.open();
getAllCat = db.getAllCategories(1);
startManagingCursor(getAllCat);
final SimpleCursorTreeAdapter cursorTreeAdapter = new SimpleCursorTreeAdapter(
getApplicationContext(),
getAllCat,
R.layout.group_row,
R.layout.group_row,
new String[]{db.KEYCAT_TXTCATEGORYNAME, db.KEYCAT_ROWID},
new int[]{R.id.groupname},
R.layout.child_row,
R.layout.child_row,
new String[]{db.KEYITEMS_TXTITEMNAME, db.KEYITEMS_ROWID, db.KEYITEMS_ROWID},
new int[]{R.id.childname,R.id.rgb}) {
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
db.open();
getMatchigItems = db.getMatchingItems(groupCursor.getLong(0), 1);
startManagingCursor(getMatchigItems);
return getMatchigItems;
}
};
expListView = getExpandableListView();
setListAdapter(cursorTreeAdapter);
tv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
db.open();
db.addMyCategory("TestCat1", "");
// getAllCat.requery();
cursorTreeAdapter.notifyDataSetChanged();
}
});
db.close();
}
}
Adding to the database works fine, but I see the new item only when restarting my app.
I have tryed to requery the cursor getAllCat.requery(); , but when I do that it refreshes the list but it’s empty (not even older item are displayed) like there is no data.
Can anyone please give me some guidance on how to resolve this?
Am I missing something?
Should I do the onResume override or should I do this completely different?
Thanks a million in advance!
Best regards!
I’m sorry. Strangely I’ve found an answer now after I’ve already asked for it here. Not my intention. But mybe someone will use it.
I’ve reinitialized my Cursor and changed it for the Adapter
This is what onClick block looks like now:
Any better suggestions are still welcome.
Thanks