Mario S
Mario S

Reputation: 301

android view.getId() returns -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.

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 ?

Upvotes: 9

Views: 4406

Answers (1)

s.maks
s.maks

Reputation: 2613

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

Upvotes: 12

Related Questions