Java – Is it possible to inflator individual elements with view inflator?

Is it possible to inflator individual elements with view inflator?… here is a solution to the problem.

Is it possible to inflator individual elements with view inflator?

Now I’m

using a custom ListView, and I’m augmenting the xml file I want to see. I want to inflate the layout according to the position to have a different inflation.

public View getView(int position,View v,ViewGroup parent) {
    if (v == null) {
        v = li.inflate(R.layout.grid_item,null);

final TextView tv = (TextView) v.findViewById(R.id.grid_text);
        tv.setText(String.valueOf(position+1));
    }

The grid items are what I’m inflating. I want to inflate several different items based on location while still maintaining the TextView. To do this at the moment, I’m changing the image in grid_item, but I’d rather do it.

Solution

Ok, so if you want to inflate different xml depending on the location, then just use some old school code I’d say

like

if (v == null) {
    if(position < 5){//whatever condition you want here    
        v = li.inflate(R.layout.grid_item,null);
    }
    else{
        v = li.inflate(R.layout.grid_item2,null);
    }
}

final TextView tv = (TextView) v.findViewById(R.id.grid_text);
tv.setText(String.valueOf(position+1));

Then make sure to name the text field grid_text in all the XML.

Related Problems and Solutions