android list array adapter.notifydatasetchanged没有正确更新视图

时间:2014-08-09 10:53:03

标签: android android-listview

在我的Android应用程序中,我试图将listitem删除为 -

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

适配器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;
}

}

我在删除项目后记录了ListView并删除了该项但未正确更新view,我的意思是只删除ListView中最后一项。

例如。如果我删除第0个索引处的项目,它将被删除,但只删除最后一个项目。我可以做错什么?

1 个答案:

答案 0 :(得分:1)

你的问题在这里:

 View v=convertView;

    if(v==null){
    }

如果转换视图不为null,则不提供if的else语句。 所以你需要改变的是:

  @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;
}
相关问题