我遇到了这个问题
java.lang.UnsupportedOperationException
我想做的很容易,我会告诉你一个例子。
蓝
红
绿色
当我在OnItemLongClickListener()
上制作Green
时,我的列表应如下所示:
绿色
我尝试的是
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);
}
});
}
});
它工作正常,如果我想要清除ListView
,但在我的情况下我不想要,我试图添加项目或删除:
adapter.addAll("Something");
或
lv.removeViewAt(2);
然后调用adapter.notifyDataSetChanged();
,但每次尝试时都会出现相同的错误。
我做错了什么?
现在代码如下:
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();
}
});
}
});
这就是LogCat错误:
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)
它指向的地方 - &gt; adapter.addAll(FileBackup.getNameFile());
但它不是null我也试过adapter.addAll("RED");
我正在尝试将名称保留为FileBackup.getFilesFound()
,因此我可以再次创建我的适配器,我正在这样做:
String[] itemvalues = (String[]) lv.getItemAtPosition(arg2);
FileBackup.setFilesFound(itemvalues);
然后我在做:
adapter.addAll(Arrays.asList(FileBackup.getFilesFound()));
//Add the name
adapter.notifyDataSetChanged();
我的班级被声明为String[]
,错误是:
java.lang.ClassCastException:java.lang.String无法强制转换为java.lang.String []
每次我想要更新列表时都会给出相同的错误... @ Y.S。说最好发布我的所有代码以查看发生了什么,因为某处我正在使用String[]
代替ArrayList<String>
这是我的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;
}
这就是我拥有文件列表的方式。
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);
}
});
我尝试过这样的事情:
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);
}
});
这就是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)
指向哪里
adapter.addAll(Arrays.asList(FileBackup.getFilesFound()));
我也试过
adapter.addAll(FileBackup.getFilesFound());
但是它给出了同样的错误......也许是我在一个不正确的地方做这件事......
答案 0 :(得分:1)
看看这个答案: ArrayAdapter
所以基本上就像在这个答案中说的那样:&#34; ArrayAdapter在被数组初始化时,将数组转换为一个无法修改的AbstractList(List)。&#34;
所以很容易,创建自己的ArrayList,在你的String类型的情况下,然后将它分配给CustomAdapter,然后你可以从ArrayList中删除项目,notifyDataSetChanged()应该工作。
此致