【发布时间】:2014-04-05 19:24:50
【问题描述】:
对于我的 listview 活动,我在 adapterView 中收到错误 unsupportedOperationException 现在我不知道如何修改数组和适配器,阅读 this 说我必须修改数组列表,但我的活动作为一个完全不同的结构请为我提供一些适用于从 listview 中删除项目的代码或我的代码的解决方案
public class DirMusic extends Activity {
ListView listSongs;
ArrayList<Song> songs;
File musicFolder;
LinearLayout songsView;
SimpleAdapter adapter;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_dir_music);
// get the list of files and place them in ListView
listSongs = (ListView) this.findViewById(R.id.listSongs);
songsView = (LinearLayout) this.findViewById(R.id.songsView);
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Confirm Delete...");
alertDialog.setMessage("Are you sure you want delete this?");
musicFolder = new File("/storage/extSdCard/MUSIC");
// musicFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
listSongs.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> view, View item,
final int position, long id) {
// Your method to play here
}
});
bindSongsToListView(musicFolder);
songsView.setVisibility(View.VISIBLE);
listSongs.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick( final AdapterView<?> arg0, View v,
final int position, long id) {
// TODO Auto-generated method stub
AlertDialog.Builder adb=new AlertDialog.Builder(DirMusic.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
listSongs.removeViewAt(position);//you can delete your item here
adapter.notifyDataSetChanged();
}});
adb.show();
return true;
}
});
}
and logCat 03-04 10:30:43.652: E/AndroidRuntime(14787): FATAL EXCEPTION: main
03-04 10:30:43.652: E/AndroidRuntime(14787): java.lang.UnsupportedOperationException: removeViewAt(int) is not supported in AdapterView
03-04 10:30:43.652: E/AndroidRuntime(14787): at android.widget.AdapterView.removeViewAt(AdapterView.java:520)
03-04 10:30:43.652: E/AndroidRuntime(14787): at com.gimi.dirmusic.DirMusic$2$1.onClick(DirMusic.java:136)
03-04 10:30:43.652: E/AndroidRuntime(14787): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
03-04 10:30:43.652: E/AndroidRuntime(14787): at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 10:30:43.652: E/AndroidRuntime(14787): at android.os.Looper.loop(Looper.java:137)
03-04 10:30:43.652: E/AndroidRuntime(14787): at android.app.ActivityThread.main(ActivityThread.java:5419)
03-04 10:30:43.652: E/AndroidRuntime(14787): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 10:30:43.652: E/AndroidRuntime(14787): at java.lang.reflect.Method.invoke(Method.java:525)
03-04 10:30:43.652: E/AndroidRuntime(14787): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-04 10:30:43.652: E/AndroidRuntime(14787): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-04 10:30:43.652: E/AndroidRuntime(14787): at dalvik.system.NativeStart.main(Native Method)
03-04 10:30:44.913: I/Process(14787): Sending signal. PID: 14787 SIG: 9
【问题讨论】:
-
你不应该从 ListView 中删除项目,你应该从它的列表中删除元素,然后刷新 ListView
-
只需从列表中删除该项目,然后使用 adapter.notifydatasetChanged()。 getView() 真是一团糟
标签: android listview android-listview android-arrayadapter