0

In my android app I am trying to delete a listitem as -

dla.remove(itemselected);
Toast.makeText(getApplicationContext(), "Pos is : "+pos+": Item is "+itemselected.getTitle(), Toast.LENGTH_SHORT).show();
dla.notifyDataSetChanged();
dla.notifyDataSetInvalidated(); 

adapter getView()

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v=convertView;
    LayoutInflater inflater=(LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(v==null){
        v=inflater.inflate(R.layout.downloaditem, parent,false);
        title=(TextView) v.findViewById(R.id.downTitle);
        status =(TextView) v.findViewById(R.id.downStatus);
        pb=(ProgressBar) v.findViewById(R.id.downprogressBar);

        title.setText(list.get(position).getTitle());
        status.setText(UnitConverter.convert(list.get(position).getDownloaded())+"/"+UnitConverter.convert(list.get(position).getFileSize())+" ("+
        list.get(position).getPercentage()+"%)");
        pb.setProgress(list.get(position).getPercentage());
        return v;
    }
else{
return v;
}

}

I have logged the ListView after removing an item and the item was removed but not updating the view correctly, I mean only the Item which is last in the ListView is removed.

For eg. if I delete item at 0th index it gets deleted but only last item is removed. What mistake could I have done?

The_ehT
  • 1,202
  • 17
  • 32
  • Try to reset the adapter, instead of calling `notifyDataSetChanged()`. May be it should work. – The Heist Aug 09 '14 at 11:01
  • Can you show where `pos` comes from – Flat Eric Aug 09 '14 at 11:01
  • @FlatEric `AdapterContextMenuInfo menuinfo=(AdapterContextMenuInfo) item.getMenuInfo(); int pos=menuinfo.position;` – The_ehT Aug 09 '14 at 11:04
  • @T-Rush I also tried recreating adapter that also didn't work – The_ehT Aug 09 '14 at 11:06
  • Maybe `itemselected` is different from the item at position `pos`. You can try `dla.remove(dla.getItem(pos));` instead – Flat Eric Aug 09 '14 at 11:08
  • can you post your getView and listView click function!! – mmlooloo Aug 09 '14 at 11:14
  • Tried it too , the `Toast` after removing shows correct position and title of the deleted item but from `ListView` only item at last posititon is ommitted. For eg. if `ListView` has 4 items if I delete 3rd item then 3rd item is deleted but its still showing in the listview however 4th item which should come at 3rd position after deleting is not shown in the list view. – The_ehT Aug 09 '14 at 11:15

1 Answers1

1

your problem is here:

 View v=convertView;

    if(v==null){
    }

you do not provide the else statement for if, if the convert view is not null you do not populate it. so all you need to change is:

  @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v=convertView;
    LayoutInflater inflater=(LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(v==null){
        v=inflater.inflate(R.layout.downloaditem, parent,false);
    }
        title=(TextView) v.findViewById(R.id.downTitle);
        status =(TextView) v.findViewById(R.id.downStatus);
        pb=(ProgressBar) v.findViewById(R.id.downprogressBar);

        title.setText(list.get(position).getTitle());
        status.setText(UnitConverter.convert(list.get(position).getDownloaded())+"/"+UnitConverter.convert(list.get(position).getFileSize())+" ("+
        list.get(position).getPercentage()+"%)");
        pb.setProgress(list.get(position).getPercentage());


    return v;
}
mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • If convertView is not null then it will not go inside if loop and return v which is not null. – The_ehT Aug 09 '14 at 11:33
  • 1
    No, convertView is not valid then, convertView is an object that makes android use less memory and GC dose not call often. this view is recycled when you scroll the view and old item that is not valid for this position is in it, so you are using it again instead of creating new object to waste memory. So when it passes to you, you must populate it in any cases but with correct data according to current position of listView. – mmlooloo Aug 09 '14 at 11:40
  • i post complete solution, are you implementing this exactly and nothing shows ? – mmlooloo Aug 09 '14 at 12:02