【问题标题】:How to use setId() for imageView如何将 setId() 用于 imageView
【发布时间】:2013-12-23 11:29:04
【问题描述】:

我在我的 GridViewAdapter 类中创建了一个 imageView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    boolean checked = (mCheckBox==null)?false:(((CheckBox)  mCheckBox).isChecked());
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(Context);
        //view.setTag(ViewId(), "imageViewGRID");
        view.setId(R.id.1);
        Log.d("GridViewAdapter", "new imageView added");
    }

但我不确定如何使用 .setId 以便我可以在这一行的另一个类中使用此 imageView:

ImageView imageView1 = (ImageView)v.findViewById(R.id.1);

有人可以解释一下这是如何工作的吗?

请注意,我也尝试过:

view.setTag("imageView4Grid");

ImageView imageView1 = (ImageView)v.findViewWithTag("imageView4Grid");

但我得到一个错误

【问题讨论】:

    标签: android


    【解决方案1】:

    嘿嘿,看看这个链接:Android: View.setID(int id) programmatically - how to avoid ID conflicts?

    从 API 17 起,View 类引入了静态方法 generateViewId(),该方法将生成适合在 setId(int) 中使用的值。

    setId(int id)simlpy 将正整数作为输入。 您正在尝试从资源中获取 id。这是你正在尝试做的:How to set Id of dynamic created layout?

    【讨论】:

    • 是的,所以我创建了一个带有 id 的 xml 文件,然后每当我在一个类中设置 id 然后尝试在另一个类中获取该视图时,它总是返回 null。我问了一个问题,但到目前为止,试图回答这个问题的人还没有回复任何反馈。顺便感谢您的链接!
    猜你喜欢
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多