Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android view.getId() returns -1

Tags:

android

view

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 ?

like image 801
Mario S Avatar asked Feb 27 '13 11:02

Mario S


1 Answers

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);
like image 172
s.maks Avatar answered Sep 20 '22 09:09

s.maks