2

I've a listview which already contain a list of data.

What I'm trying to achieve is when I click one of the ListItem, I want to add another bunch of dataset just below the clicked item.

protected void onListItemClick(ListView l, View v, int position, long id) {
    if (position == 0) {
        /* 
         * 
         * want to add another bunch of data just below postion 0!!!
         * 
        */
    }
}
3
  • what adapter are you using? Commented Jan 9, 2013 at 9:50
  • 1
    check @sapan answer then. You can also directly reference the adapter, so you don't have to do those cast Commented Jan 9, 2013 at 10:25
  • you want to add data like sub children for the selected item or just as siblings on the same level if as siblings then check @sapan answer Commented Jan 9, 2013 at 10:44

2 Answers 2

9

You can insert the data in the adapter that you use with list view and then call the notifyDataSetChanged() on the adapter to update the list view. You should use an ArrayAdapter (or its subclass) to be able to dynamically add objects to the list view.

((ArrayAdapter)listView.getAdapter()).insert(object, index);
((ArrayAdapter)listView.getAdapter()).notifyDataSetChanged();
Sign up to request clarification or add additional context in comments.

1 Comment

check the insert (T object, int index) ref developer.android.com/reference/android/widget/…, is that normal you try to insert an array of string?
1

Have a look at ExpandableListView

Another tutorial

1 Comment

thanks for the suggestion @jafar. though what I want is a little bit different from ExpandableListView.

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.