0

I am extending Android BaseAdapter.

In regards to method

getView (int position, View convertView, ViewGroup parent)

The docs say

You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.

I am wondering how do I check if the View is of appropriate type?

Lets say I would expect a LinearLayout, with two TextView children? How would I check this properly?

Would this be correct?

if (convertView instanceof LinearLayout) {

if (convertView.findViewById(someid) != null) {
//its what I expect...
}

}

Scorb
  • 1,918
  • 7
  • 38
  • 81
  • If you have more than 1 view, you can use holder. Check this [Answer](http://stackoverflow.com/a/19289890/5573623). – Eliran Tutia May 10 '16 at 01:52

2 Answers2

1

If you only have 1 view type in your adapter, then you only need to check if it is non null.

Checking view types only applies if you if you have multiple view types (when you override getViewTypeCount() to return anything other than 1), then you need to figure out which type it is based on the position. Basically, if you do not override getViewTypeCount() then just check for null.

Francesc
  • 13,308
  • 4
  • 34
  • 50
0

Creating a ViewHolder class would be appropriate if you have multiple views this can help you with getting the view on the layout during run time as you are using a holder with each view

Pooja Nair
  • 113
  • 7