8

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.

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);

        }  
    }
...     
}

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)

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

Mario S
  • 291
  • 1
  • 8
  • 18

1 Answers1

11

From the Android LayoutInflater documentation:

Inflate a new view hierarchy from the specified xml resource.

So when you call

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

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

converView.setId(YOUR_GENERATED_ID);
s.maks
  • 2,583
  • 1
  • 23
  • 27
  • 1
    This is a great answer and it helped me tremendously. Thank you! I did not realize that the internal view id assigned by xml inflater is not accessible and needs to be assigned manually again. I have now successfully completed the task of updating the header cell titles at runtime. – Mario S Feb 28 '13 at 06:48