-2

I'm trying to use the listview with sectionheaders as mentioned here: Android ListView headers

The values are populated dynamically as shown below:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult called");
    if((requestCode == 1)&&(resultCode == Activity.RESULT_OK)){
        if(data!= null){
            AddedTask=data.getStringExtra("NewTask");
            CategoryName=data.getStringExtra("CategoryItem");
            TaskTime=data.getStringExtra("TaskTime");
            List<Item> items = new ArrayList<Item>();
                items.add(new Header(CategoryName));
                items.add(new ListItem(AddedTask, TaskTime));
                TwoTextArrayAdapter adapter = new TwoTextArrayAdapter(getActivity(), items);
                listViewData.setAdapter(adapter);
                adapter.notifyDataSetChanged();
        }
    }
}

My Problem is when I add the values first time its adding perfect. But when I get the values second time the values are simply updating with the previous values and not showing me the first added values.

Community
  • 1
  • 1
coder
  • 13,002
  • 31
  • 112
  • 214
  • 1
    well of course. `items` contains what you are returning from the other activity, and nothing more. You are also instantiating a new `TwoTextArrayAdapter` and, calling `setAdapter`, you are replacing the old one. `notifyDataSetChanged` does nothing in this case, but it is not its fault – Blackbelt May 17 '15 at 14:03
  • @Blacbelt- Ah silly! I havent gone through the silly mistake I have done. Got working now..but the problem is each time I addthe Item it dosent check of the header is same or not and add the new list to the existing header – coder May 17 '15 at 15:08

2 Answers2

1

This line has issue

List<Item> items = new ArrayList<Item>();

Every time it creates a new List and the previous values are cleared

Declare it outside a method

codeMagic
  • 44,549
  • 13
  • 77
  • 93
Deepanshu Gandhi
  • 490
  • 3
  • 10
1


When you add some items in the second time, they will replace with the first one, because you declare a new "items" every time.
You should declare "items" as a public variable outside the onActivityResult function.
You can do something like this:

List<Item> items;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult called");
if((requestCode == 1)&&(resultCode == Activity.RESULT_OK)){
    if(data!= null){
        AddedTask=data.getStringExtra("NewTask");
        CategoryName=data.getStringExtra("CategoryItem");
        TaskTime=data.getStringExtra("TaskTime");
        if (items == null)
            items = new ArrayList<Item>();
        items.add(new Header(CategoryName));
            items.add(new ListItem(AddedTask, TaskTime));
            TwoTextArrayAdapter adapter = new TwoTextArrayAdapter(getActivity(), items);
            listViewData.setAdapter(adapter);
            adapter.notifyDataSetChanged();
    }
}
}
Fartab
  • 4,725
  • 2
  • 26
  • 39