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

时间:2015-01-06 20:46:30

标签: android android-listview notifydatasetchanged

我创建了一个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;
}

4 个答案:

答案 0 :(得分:2)

问题是您的ArrayAdapterString[] web支持,因此即使您删除Fileweb仍然保持不变。将web转换为List<String>,删除文件后,在web.remove(position)之前致电notifyDataSetChanged()

答案 1 :(得分:1)

实施getCount()方法

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

    }

答案 2 :(得分:1)

你这样做是错误的。不要在Adapter中传递所有这些数组。像这样创建类(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())
       ...
       ...
    }
}

那就是它。

答案 3 :(得分:0)

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