2012-03-04 89 views
2

我有這樣的代碼我習慣一個數組列表傳遞到另一個頁面,並顯示它作爲一個列表視圖。當列表出現時,我希望能夠檢查一個項目,並在「按鈕點擊」將其修改數組中刪除它。如何刪除選中的項目/特定項目從一個數組列表/列表視圖

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.oppout); 

    final ListView lv2 = (ListView) findViewById (R.id.custom_list_view); 
    lv2.setClickable(true); 
    lv2.setAdapter(new ArrayAdapter<String>(Oppout.this, 
              android.R.layout.simple_list_item_checked, 
              Entername.playerList)); 

    lv2.setOnItemClickListener (
     new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView adapterView, 
            View view,int arg2, long arg3) { 
       int selectedPosition = adapterView.getSelectedItemPosition();         
       Toast.makeText(getBaseContext(), "mu"+ selectedPosition, 
          Toast.LENGTH_SHORT).show();        
      } 
    }); 


    Button next = (Button) findViewById(R.id.button1); 
    next.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      // on click call the adapterview and delete string at selected position. 
      // This is my problem, am not getting how to call the adapter and deleted 
      // the selected item/position 
      int selectedPosition = adapterView.getSelectedItemPosition(); 
      adapterView.remove(player.SelectedPosition); 

      Intent myIntent = new Intent (view.getContext(), Callacab.class); 
      startActivityForResult(myIntent, 0); 
     } 
    }); 
}} 
+0

@Aleks G中ARG2,什麼也û編輯? – user1179083 2012-03-04 18:22:35

+0

我更新了最後一種方法的格式和縮進。 – 2012-03-04 18:24:35

+0

點擊標籤按鈕下面的「編輯」鏈接,你會看到差異。 – 2012-03-06 07:27:37

回答

0

contacts.remove(index); // ArrayList中,你把它給你的適配器

arrayadapter.remove(指數); //這是你給它的列表視圖的適配器 arrayadapter.notifyDataSetChanged();

//你可以從你的ArrayList或適配器,然後notifyDataSetChanged()和刪除;使你的列表視圖效果是....它更好地從ArrayList中刪除其指數法將在您的列表視圖項聽衆