机器人view.getId()返回-1 [英] android view.getId() returns -1

查看:2591
本文介绍了机器人view.getId()返回-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要改变文本的一系列头单元格中的数据表。
为了实现这一目标,我需要检索和存储分配给这些细胞中的视图ID,这样他们就可以在以后使用,以确定这些细胞的看法。
但是,当我试图得到一个ID为刚刚充电池来看,它总是返回-1。

I need to change the text for a range of header cells in the data table.
In order to achieve that, I would need to retrieve and store the view ids assigned to those cells, so they can be used later to identify those cell views.
However, when I try to get an ID for the just inflated cell view, it always returns -1.

下面是我的示例code:

Here is my sample code:

@Override
public View getView(final int row, final int column, View converView, ViewGroup parent) {
    if (converView == null) {
        converView = inflater.inflate(getLayoutResource(row, column), parent, false);
        if (row == -1){
            int viewId = converView.getId();
            setHeaderId(viewId, column+1);

        }  
    }
...     
}

在converView.getId()在code以上返回-1,尽管视图ID已经被分配,并且是code调试过程中可见的事实。例如,我可以看到在调试过程如下: converView =的LinearLayout(ID = 830042440680)

The converView.getId() in the code above returns -1, despite the fact that the view id has already been assigned and is viewable during the code debugging. For example, I can see the following during debug: converView= LinearLayout (id= 830042440680)

任何想法,为什么我收到-1(= NO_ID)在上述code?

Any idea why I am getting -1 (= NO_ID) in the above code ?

推荐答案

从Android LayoutInflater文档:

From the Android LayoutInflater documentation:

从指定的XML资源充气一个新的视图层次。

Inflate a new view hierarchy from the specified xml resource.

所以,当你调用

converView = inflater.inflate(getLayoutResource(row, column), parent, false);

converView仅通过布局充气创造,没有身份证。您应该手动设置ID这样

converView is just create by your layout inflater and have no ID. You should set ID manually like this

converView.setId(YOUR_GENERATED_ID);

这篇关于机器人view.getId()返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆