Unable to modify values in ListView

157 views Asked by At

I'm faced with this problem

java.lang.UnsupportedOperationException

What I want to do is easy, I'll show you an example.

MyList

Blue
Red
Green

When I make an OnItemLongClickListener() for example on Green, my list should looks like :

MyList

Green

What I've tried is

final ArrayAdapter < String > adapter = new ArrayAdapter < String > (getActivity(), android.R.layout.simple_list_item_1, FileBackup.getFilesFound());
adapter.setNotifyOnChange(true);
lv.setAdapter(adapter);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView <? > arg0, View arg1, final int arg2, long arg3) {
        String itemValue = (String) lv.getItemAtPosition(arg2);
        Log.d("Item clicked --> ", lv.getItemAtPosition(arg2).toString());
        FileBackup.setNameFile(itemValue);
        final Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.user_file_choose);
        Button button = (Button) dialog.findViewById(R.id.btOk);
        Button button2 = (Button) dialog.findViewById(R.id.btKo);
        TextView tvBackUpFile = (TextView) dialog.findViewById(R.id.textViewContentDialog);
        tvBackUpFile.setText("Do you want to backup " + FileBackup.getNameFile() + " ?");
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();
                //Clear all the ListView
                lv.setAdapter(null);

            }
        });
    }
});

It's working fine, if I want to clear the ListView, but in my case I DON'T WANT TO, I tried to add item or remove with :

adapter.addAll("Something");

or

lv.removeViewAt(2);

And then call adapter.notifyDataSetChanged(); but it gives the same error every time I try to.

What I'm doing wrong?

EDIT

Now the code looks like :

final ArrayAdapter < String > adapter = new ArrayAdapter < String > (getActivity(), Arrays.asList(FileBackup.getFilesFound()));
adapter.setNotifyOnChange(true);
lv.setAdapter(adapter);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView <? > arg0, View arg1, final int arg2, long arg3) {
        String itemValue = (String) lv.getItemAtPosition(arg2);
        Log.d("Item clicked --> ", lv.getItemAtPosition(arg2).toString());
        FileBackup.setNameFile(itemValue);
        final Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.user_file_choose);
        Button button = (Button) dialog.findViewById(R.id.btOk);
        Button button2 = (Button) dialog.findViewById(R.id.btKo);
        TextView tvBackUpFile = (TextView) dialog.findViewById(R.id.textViewContentDialog);
        tvBackUpFile.setText("Do you want to backup " + FileBackup.getNameFile() + " ?");
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();
                //Clear all the ListView
                lv.setAdapter(null);
                //Trying to add that current item clicked
                adapter.addAll(FileBackup.getNameFile());
                adapter.notifyDataSetChanged();

            }
        });
    }
});

And this is the LogCat error :

06-10 20:14:43.531    8072-8072/info.androidhive.tabsswipe E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: info.androidhive.tabsswipe, PID: 8072
java.lang.UnsupportedOperationException
        at java.util.AbstractList.add(AbstractList.java:404)
        at java.util.AbstractList.add(AbstractList.java:425)
        at java.util.Collections.addAll(Collections.java:2583)
        at android.widget.ArrayAdapter.addAll(ArrayAdapter.java:211)
        at info.androidhive.tabsswipe.BackupWA$1$1.onClick(BackupWA.java:80)
        at android.view.View.performClick(View.java:4456)
        at android.view.View$PerformClick.run(View.java:18462)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5102)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

Where it's pointing this --> adapter.addAll(FileBackup.getNameFile()); but it's not null I also tried to adapter.addAll("RED");

EDIT2

I'm trying to keep the name what I've clicked to FileBackup.getFilesFound() so I can create my adapter again I'm doing it with :

String[] itemvalues = (String[]) lv.getItemAtPosition(arg2);
FileBackup.setFilesFound(itemvalues);

And then I'm doing :

 adapter.addAll(Arrays.asList(FileBackup.getFilesFound()));
 //Add the name
 adapter.notifyDataSetChanged();

On my class is declared as String[] and the error is :

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.String[]

EDIT3

It's giving the same error every time I want to update the list... @Y.S. said that It's better to post all of my code to see what's going on, because somewhere I'm using String[] instead of ArrayList<String>

That's my FileBackup.java

    public class FileBackup {

    private String path;
    private String pathFile;
    private File FileToBackUp;
    private String SDpath;
    private String[] FilesFound;
    private String nameFile;

    public String getPathFile() {
        return pathFile;
    }

    public void setPathFile(String pathFile) {
        this.pathFile = pathFile;
    }
    public String getNameFile() {
        return nameFile;
    }

    public void setNameFile(String nameFile) {
        this.nameFile = nameFile;
    }

    public FileBackup(String path){
        this.path = path;
    }

    public File getFileToBackUp() {
        return FileToBackUp;
    }

    public void setFileToBackUp(File fileToBackUp) {
        FileToBackUp = fileToBackUp;
    }
public String[] getFilesFound() {
       this.FilesFound = FilesFound();

        return FilesFound;
    }
 public String[] FilesFound() {
        File dir = new File(this.path);
        File[] filelist = dir.listFiles();
        String[] theNamesOfFiles = new String[filelist.length];
        for (int i = 0; i < theNamesOfFiles.length; i++) {

            theNamesOfFiles[i] = filelist[i].getName();
        }

        return theNamesOfFiles;
    }

And this is how I have the List of the files.

final String itemValue = (String) lv.getItemAtPosition(arg2);

 Log.d("Item clicked --> ", lv.getItemAtPosition(arg2).toString());


 final Dialog dialog = new Dialog(getActivity());
 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
 dialog.setContentView(R.layout.user_file_choose);
 Button button = (Button) dialog.findViewById(R.id.btOk);
 Button button2 = (Button) dialog.findViewById(R.id.btKo);
 FileBackup.setNameFile(itemValue);
 TextView tvBackUpFile = (TextView) dialog.findViewById(R.id.textViewContentDialog);
 tvBackUpFile.setText("Do you want to backup " + FileBackup.getNameFile() + " ?");
 button.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
         //The complete path of the fail
         FileBackup.setPathFile(GlobalConstant.path + "/" + FileBackup.getNameFile());
         //Converting the path of the file to a File and putting it to FileToBackUp
         FileBackup.setFileToBackUp(PathToFile(FileBackup.getPathFile()));

         dialog.dismiss();
         //Clear all the ListView
         lv.setAdapter(null);

         //Add the name
         BackUpFile.setEnabled(true);


     }
 });

I tried to do something like :

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //The complete path of the fail
        FileBackup.setPathFile(GlobalConstant.path + "/" + FileBackup.getNameFile());
        //Converting the path of the file to a File and putting it to FileToBackUp
        FileBackup.setFileToBackUp(PathToFile(FileBackup.getPathFile()));
        ///// THIS IS NEW ///////////
        String itemvalues = (String) lv.getItemAtPosition(arg2);
        FileBackup.setFilesFound(new String[] {
            itemvalues
        });
        adapter.addAll(FileBackup.getFilesFound());
        //Add the name
        adapter.notifyDataSetChanged();
        //////////TIL HERE/////////////
        dialog.dismiss();
        //Clear all the ListView


        //Add the name
        BackUpFile.setEnabled(true);


    }
});

And this is the LogCat

 java.lang.UnsupportedOperationException
        at java.util.AbstractList.add(AbstractList.java:404)
        at java.util.AbstractList.add(AbstractList.java:425)
        at java.util.AbstractCollection.addAll(AbstractCollection.java:76)
        at android.widget.ArrayAdapter.addAll(ArrayAdapter.java:195)
        at info.androidhive.tabsswipe.BackupWA$1$1.onClick(BackupWA.java:88)
        at android.view.View.performClick(View.java:4456)
        at android.view.View$PerformClick.run(View.java:18462)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5102)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

Where is pointing

adapter.addAll(Arrays.asList(FileBackup.getFilesFound()));

Also I've tried with

adapter.addAll(FileBackup.getFilesFound());

But it's giving the same error... Maybe is that I'm doing this on a not correct place...

1

There are 1 answers

0
Max Pinto On BEST ANSWER

Look this answer: ArrayAdapter

So basically as in this answer said: "The ArrayAdapter, on being initialized by an array, converts the array into a AbstractList (List) which cannot be modified."

So it easy, create your own ArrayList, in your case of type String, and then assign this to CustomAdapter, then you can just remove items from your ArrayList and notifyDataSetChanged() should works.

Regards