Unable to change values ​​in ListView

I ran into this problem

java.lang.UnsupportedOperationException

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

Mylist

Blue
Red
Green

When I do OnItemLongClickListener() , for example, in Green , my list should look like this:

Mylist

Green

What I tried

 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 works fine if I want to clear the ListView , but in my case I DO NOT WANT, I tried to add an item or delete using:

 adapter.addAll("Something"); 

or

 lv.removeViewAt(2); 

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

What am I doing wrong?

EDIT

Now the code looks like this:

 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 a 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 he points to this - adapter.addAll(FileBackup.getNameFile()); but it is not empty, I also tried adapter.addAll("RED");

EDIT2

I'm trying to save the name that I clicked on the FileBackup.getFilesFound() button to create my adapter again. I do it with

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

And then I do:

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

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

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

EDIT3

It gives the same error every time I want to update the list ... @YS said it is better to post all my code to find out what is happening, because somewhere I use String[] instead of ArrayList<String>

What is 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 so I have a list of 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 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 indicated

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

I also tried with

 adapter.addAll(FileBackup.getFilesFound()); 

But it gives the same error ... Maybe I'm doing it in the wrong place ...

0
java android android-listview android-arrayadapter android-adapter
Jun 10 '15 at 17:44
source share
1 answer

Check out this answer: ArrayAdapter

Since this answer says: "ArrayAdapter, being initialized by an array, converts the array into an abstract List, which cannot be changed."

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

Hello

+1
Jun 10 '15 at 17:54
source share



All Articles