To my listview activity I am getting error unsupportedOperationException in adapterView now I have no Idea on How to modify the array and adapter, reading this says that I have to modify the array list but my activity as a completely different structure please provide me some code that works for deletind items from listview or a solution for my code
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