【发布时间】:2012-07-02 21:15:15
【问题描述】:
我有 CustomAdapter 扩展 ArrayAdapter,在其中我在 LinearLayout 中设置了一些视图 - 一行,像这样(适配器):
public View getView(int position, View convertView, ViewGroup parent) {
. . .
ImageView statePic = (ImageView) view.findViewById(R.id.state);
statePic.setImageResource(R.drawable.light_green);
return view;
}
这很好用。但是现在我想在我的列表中的项目上绑定一个上下文菜单,所以我有这样的东西(主要活动 - listActivity):
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()){
case CM_START:
View view = getListAdapter().getView(info.position, null, null);
ImageView statePic = (ImageView)view.findViewById(R.id.state);
// I'm sure this view is the right one
statePic.setImageResource(R.drawable.light_grey);
view.invalidate();
view.refreshDrawableState();
return true;
case CM_STOP:
return true;
}
return super.onContextItemSelected(item);
}
我想用这个 sn-p 实现的是更改该项目中的图像,但这不起作用(视图不会用新值刷新)。我该怎么做?
【问题讨论】:
-
请使用
android-listview标签而不是listview:) -
好的,很抱歉 :-)
-
究竟是什么不起作用? ContextMenu的绑定不起作用还是改变ImageView中的资源?
-
绑定正常工作,更改资源不起作用。或者刷新该视图 - 如果我从同一个视图读取,则有正确的资源集。
标签: android android-listview custom-adapter