【问题标题】:How to change a view property in CustomAdapter?如何更改 CustomAdapter 中的视图属性?
【发布时间】: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


【解决方案1】:

您应该使用 AdapterContextMenuInfo 类的 targetView 属性:

...
ImageView statePic = (ImageView) info.targetView.findViewById(R.id.state);
...

【讨论】:

    猜你喜欢
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    相关资源
    最近更新 更多