Android : notifyDataSetChanged() 在删除元素后不更新 ListView

Android : notifyDataSetChanged() not updating the ListView after deleting an element

我创建了一个 ListView,它由目录中的文件列表组成。 每行都有一个按钮,用户可以通过该按钮删除文件。 删除文件后,我想更新列表视图,以便该行不再可用。 我尝试使用 notifyDataSetChanged() 但它没有更新列表。

public class CustomList extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] web;
    private final Integer[] imageId;
    private final String[] datetime;
    public CustomList(Activity context,
                      String[] web, Integer[] imageId,String[] datetime) {
        super(context, R.layout.list_single, web);
        this.context = context;
        this.web = web;
        this.imageId = imageId;
        this.datetime=datetime;
    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        final View rowView= inflater.inflate(R.layout.list_single, null, true);
        final TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        TextView txtTitle2 = (TextView) rowView.findViewById(R.id.txt1);
        txtTitle.setText(web[position]);
        imageView.setImageResource(R.drawable.ic_launcher);
        txtTitle2.setText(datetime[position]);

        final Button button=(Button)rowView.findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String text=txtTitle.getText().toString();
                String text1= Environment.getExternalStorageDirectory().getAbsolutePath().toString() +"/Notate/"+text+".pcm";
                Toast.makeText(getContext(), text1, Toast.LENGTH_LONG).show();
                File filetodelete=new File(text1);
                filetodelete.delete();
                button.setText("Deleted");
                button.setEnabled(false);
                adapter.notifyDataSetChanged();


            }
        });
        return rowView;
    }
}

ListView list;
public FragmentB() {
    // Required empty public constructor
}
CustomList adapter=null;

String[] days;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    String path = Environment.getExternalStorageDirectory().toString()+"/Notate";
    File f = new File(path);
    File file[] = f.listFiles();
    days=new String[file.length];
    Integer[] imageId=new Integer[file.length];
    String[] dateTime=new String[file.length];

    for (int i=0; i < file.length; i++)
    {
        String temp=file[i].getName();
        String temp2=temp.substring(0,temp.length()-4);
        Date lastModDate = new Date(file[i].lastModified());

        days[i]=temp2;
        imageId[i]=R.drawable.ic_launcher;
        dateTime[i]=lastModDate.toString().substring(0,lastModDate.toString().length()-14);
    }

    // Inflate the layout for this fragment
    final View contextView = inflater.inflate(R.layout.fragment_fragment_c,container,false);
    adapter = new
            CustomList(this.getActivity(), days, imageId,dateTime);
    list=(ListView) contextView.findViewById(R.id.listView);
    list.setAdapter(adapter);
list.setOnItemClickListener(this);
    return contextView;
}

实现 getCount() 方法

@Override
    public int getCount() {
return objects.size();

    }

你做错了。不要在适配器中传递所有这些数组。像这样创建 Class (POJO):

public class MyClass {
 private String web;
 private Integer imageId;
 .....
}

创建这些对象的列表,例如List<MyClass> myClasses = new ArrayList<>(),填入合适的数据。

然后将此列表传递给您的适配器并在您的 getView 方法中使用该数据:

public class CustomList extends ArrayAdapter<MyClass> {
     public CustomList(Activity context, List<MyClass> myClasses) {
         super(context, R.layout.list_single, myClasses;
     }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        MyClass myClass = getItem(position); //Take a look here
       (TextView) rowView.findViewById(R.id.txt).setText(myClass.getWeb())
       ...
       ...
    }
}

就是这样。

问题是您的 ArrayAdapter 得到了 String[] web 的支持,因此即使您删除了 Fileweb 仍然保持不变。将 web 转换为 List<String> 并在删除文件时在 notifyDataSetChanged().

之前调用 web.remove(position)

您刚刚删除了文件。您还需要从数组中删除条目。