我有一个arraylist,其中长按一个元素,它显示一个上下文菜单,我可以删除或编辑arraylist的选定项目。
我有疑问,我如何定义我选择的项目,编辑或删除它?
这些是上下文菜单中引用的方法。这是我第一次使用这个菜单而我还没有测试过功能,所以这些方法可能都是错误的。
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info;
info = (AdapterView.AdapterContextMenuInfo) menuInfo;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit_travel:
return true;
case R.id.delete_travel:
return true;
default:
return super.onContextItemSelected(item);
}
}
正如我所说,我认为错过了确定我想编辑或删除哪个arraylist元素的方法,但我不知道如何定义
更新 - 添加代码
public class TravelListActivity extends ListActivity {
private static final int REQUEST_CODE_CREATE_TRAVEL = 0;
private TravelAdapter adapter;
private class TravelAdapter extends ArrayAdapter<TravelInfo>{
private final Context context;
private final ArrayList<TravelInfo> travels;
private static final int RESOURCE = android.R.layout.simple_list_item_2;
public TravelAdapter(Context context, ArrayList<TravelInfo> travels) {
super(context, RESOURCE, travels);
this.context = context;
this.travels = travels;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout view;
ViewHolder holder;
if (convertView == null){
view = new LinearLayout(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(RESOURCE, view, true);
holder = new ViewHolder();
holder.text1 = (TextView) view.findViewById(android.R.id.text1);
holder.text2 = (TextView) view.findViewById(android.R.id.text2);
view.setTag(holder);
} else {
view = (LinearLayout) convertView;
holder = (ViewHolder) view.getTag();
}
TravelInfo info = travels.get(position);
holder.text1.setText(info.getCity() + " (" + info.getCountry() + ")");
holder.text2.setText(getResources().getString(R.string.year) + " " + info.getYear());
return view;
}
}
static class ViewHolder {
TextView text1;
TextView text2;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerForContextMenu(getListView());
答案 0 :(得分:0)
您可以通过以下行获得item
长按:
TravelInfo itemSelected = adapter.getItem(info.position);
示例:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
TravelInfo itemSelected = adapter.getItem(info.position);
switch (item.getItemId()) {
case R.id.edit_travel:
return true;
case R.id.delete_travel:
return true;
default:
return super.onContextItemSelected(item);
}
}
注意:要使其工作(1),您应该通过调用adapter
<onCreate
(2)设置列表适配器初始化setAdapter(adapter)
/ p>
答案 1 :(得分:0)
您可以获取所选行列表视图的位置:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit_travel:
Toast.makeText(getBaseContext(), "edit: "+info.position, Toast.LENGTH_SHORT).show();
return true;
case R.id.delete_travel:
Toast.makeText(getBaseContext(), "delete: "+info.position, Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
获取位置后,从该位置的Arraylist更新它。
现在,请致电notifyDataSetChanged :: youradapter.notifyDataSetChanged();