android中在onContextItemSelected方法中怎样获取itemId

小邪 发布于 2013/08/15 15:16
阅读 1K+
收藏 0

ListView是用的自定义的adapter,代码如下:


public class RuleListAdapter extends BaseAdapter {
	private Context context;
	
	private List<RuleEntry> rules;
	
	public RuleListAdapter(Context context,List<RuleEntry> rules) {
		this.context = context;
		this.rules = rules;
	}
	
	@Override
	public int getCount() {
		return rules.size();
	}

	@Override
	public Object getItem(int position) {
		return rules.get(position);
	}

	@Override
	public long getItemId(int position) {
		return rules.get(position).getId();
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		if(view == null){
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.rule_item, null);
		}
		
		RuleEntry e = rules.get(position);
		
		if (Constants.BLACK.equals(e.getRuleType())) {
			e.setRuleType(context.getResources().getString(
					R.string.rule_black));
		} else if(Constants.WHITE.equals(e.getRuleType())) {
			e.setRuleType(context.getResources().getString(
					R.string.rule_white));
		}
		if (Constants.PHONE.equals(e.getRuleClass())) {
			e.setRuleClass(context.getResources()
					.getString(R.string.rule_phone));
		} else if(Constants.SMS.equals(e.getRuleClass())){
			e.setRuleClass(context.getResources().getString(R.string.rule_sms));
		}
		
		String t = context.getResources().getString(R.string.rule_type);
		String c = context.getResources().getString(R.string.rule_con);
		
		TextView classView = (TextView)view.findViewById(R.id.rule_list_item_title);
		classView.setText(e.getRuleClass());
		
		TextView typeView = (TextView)view.findViewById(R.id.rule_list_item_type);
		typeView.setText(t + e.getRuleType() + "/" + e.getRuleEffect());
		
		TextView conView = (TextView)view.findViewById(R.id.rule_list_item_con);
		conView.setText(c + e.getRuleCon());
		
		return view;
	}
	
}
现在只能获取到position,是用下面的方法:



AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

int position = info.position;
有没有方法直接获取到itemId,我不想在List<RuleEntry>中再找了。


加载中
返回顶部
顶部