到我的listview活动我在adapterView中收到错误unsupportedOperationException现在我对如何修改数组和适配器一无所知,阅读this说我必须修改数组列表但我的活动完全不同结构请提供一些适用于listview的deletind项目或代码解决方案的代码
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
答案 0 :(得分:2)
要删除列表视图的任何元素,您应该删除适配器中数组列表的元素。为此你可以创建一个方法removeItem(position)并从适配器数组中删除item,然后调用notifyDatasetChanged()。
我希望它会对你有所帮助。
答案 1 :(得分:0)
ImageAdapter mImageAdapter;
//...
int pos;
// set pos in range 0 to mImageAdapter.getCount()-1
mImageAdapter.itemList.remove(pos);
mImageAdapter.notifyDataSetChanged();
答案 2 :(得分:0)
对于最后一个项目没有被删除的问题,请更换
recyclerView.removeViewAt(position)
与
linearLayoutAdapter.removeViewAt(position)