Search code examples
androidandroid-listviewnotifydatasetchanged

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


I have created a ListView which consists of a list of files in a directory. Every row has a button through which the user can delete the file. After deletion of the file, i want to update the listview so that the row is no more available. i tried to use the notifyDataSetChanged() but it did not update the list.

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

Solution

  • The problem is that your ArrayAdapter is backed by String[] web so even if you delete a File, web remains the same. Convert web to a List<String> and when the file is deleted call web.remove(position) before notifyDataSetChanged().