2012-11-11 46 views
0

我想從ListView中刪除數據。對於長按活動,我已使用以下代碼:使用remove方法拋出ListView的ArrayAdapter拋出UnsupportedOperationException

 lstGame.setOnItemLongClickListener(new OnItemLongClickListener() { 

     @Override 
     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) { 
      AlertDialog.Builder builder = new AlertDialog.Builder(FavouriteActivity.this); 
      builder.setMessage("Remove from Favourite?").setCancelable(false) 
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          Const.favourite(FavouriteActivity.this, (args[arg2])); 
           Toast.makeText(FavouriteActivity.this, "Selected Item Removed from Favourite.", Toast.LENGTH_LONG).show(); 
           // Here I get the UnsupportedException----> 
           // adapter.remove(args[arg2]); 
           lstGame.setAdapter(adapter); 
           lstGame.invalidate(); 

         } 
        }).setNegativeButton("No", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
      Dialog alert = builder.create(); 
      alert.show(); 
      return false; 
     } 
    }); 

爲什麼我會得到該異常?

+0

請分享logcat日誌.. –

回答

1

如果適配器參考點爲默認ArrayAdapter實例,那麼你最有可能實例化ArrayAdapter使用對象的array作爲其數據源。如果是這種情況,那麼在底層,ArrayAdapter會將該陣列轉換爲特殊的ArrayList(不是普通的java)。這種特殊的ArrayList不執行該改變其大小(因此使用方法,如addremove(其中修改列表)上的ArrayAdapter將引發UnsupportedOperationException),只會讓你修改它的值的方法。

如果你想使用remove方法然後把數據從您在ArrayListArrayAdapter目前使用,然後通過該列表的ArrayAdapter構造的array

相關問題