2012-03-04 51 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中删除其指数法将在您的列表视图项听众