无法在ListView中修改值 [英] Unable to modify values in ListView
问题描述
我面对这个问题。
java.lang.UnsupportedOperationException
java.lang.UnsupportedOperationException
我想要做的是容易的,我会告诉你一个例子。
What I want to do is easy, I'll show you an example.
蓝
结果红
结果绿
Blue
Red
Green
当我提出一个 OnItemLongClickListener()
例如在绿色
,我的名单应该是这样的:
When I make an OnItemLongClickListener()
for example on Green
, my list should looks like :
绿色
我已经试过是
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
,但在我来说,我不想,我试图添加项目或删除:
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");
或
lv.removeViewAt(2);
然后调用 adapter.notifyDataSetChanged();
,但它给出了同样的错误,每次我试图以
And then call adapter.notifyDataSetChanged();
but it gives the same error every time I try to.
我在做什么错了?
现在的code如下:
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中的错误:
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)
,但它不是空我也试图 当它指向这一点。的addAll(RED); 适配器;>
adapter.addAll(FileBackup.getNameFile()) -
我试图让我点击到什么名字FileBackup.getFilesFound()
这样我就可以重新创建我的适配器,我有这样做:
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);
然后我做的:
adapter.addAll(Arrays.asList(FileBackup.getFilesFound()));
//Add the name
adapter.notifyDataSetChanged();
在我的类被声明为的String []
和错误是:
On my class is declared as String[]
and the error is :
java.lang.ClassCastException:java.lang.String中不能转换为java.lang.String []
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.String[]
它给了同样的错误我想更新列表... @ Y.S每次。说,这是更好地后我所有的code,看看发生了什么事情,因为地方我使用的String []
而不是的ArrayList&LT ;弦乐&GT;
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>
这是我的 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);
}
});
这是LogCat中
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)
在哪里指向
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...
推荐答案
看这样的回答:
<一href=\"http://stackoverflow.com/questions/3200551/unable-to-modify-arrayadapter-in-listview-unsupportedoperationexception\">ArrayAdapter
所以基本上在这个答案说:。一个ArrayAdapter,对数组初始化,转换阵列成AbstractList中(列表),它不能被修改的
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."
所以很容易,创建你自己的ArrayList,在您的String类型的情况下,然后分配这CustomAdapter,那么你可以删除您ArrayList和notifyDataSetChanged(项目)应该工作。
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.
问候
这篇关于无法在ListView中修改值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!