The_ehT
The_ehT

Reputation: 1230

android list array adapter.notifydatasetchanged not updating view correctly

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?

Upvotes: 0

Views: 723

Answers (1)

mmlooloo
mmlooloo

Reputation: 18977

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;
}

Upvotes: 1

Related Questions