public class MainActivity extends ActionBarActivity {
ListView listview;
String[] subjects = new String[] {"Android","PHP","Blogger","WordPress","SEO"};
List<String> subject_list;
ArrayAdapter<String> arrayadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView)findViewById(R.id.listView1);
subject_list = new ArrayList<String>(Arrays.asList(subjects));
arrayadapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, subject_list);
listview.setAdapter(arrayadapter);
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
subject_list.remove(position);
arrayadapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Item Deleted", Toast.LENGTH_LONG).show();
return true;
}
});
}
}
这里我有一个程序,通过长按,从列表视图中删除项目,而不是删除项目必须通过长按ListView
中的项目来禁用项目。
答案 0 :(得分:0)
试试这个:
subject_list.getChildAt(position).setEnabled(false);
:):)
答案 1 :(得分:0)
尝试以下代码
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
//subject_list.remove(position);
if(!view.isEnabled()){
return false;
}
view.setEnabled(false);
view.setClickable(false);
arrayadapter.notifyDataSetChanged();
Toast.makeText(Main2Activity.this, "Item Deleted", Toast.LENGTH_LONG).show();
return true;
}
});
由于listview已被RecyclerView取代,所以我建议您将来使用RecyclerView或者使用它。
快乐编码:)