0

I have a ListView where I would like to inflate a custom View at a certain position. It's actually the 11th item (position 10).

The getCount() method returns 11.

Here is the top part of the getView() method:

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {              
        if (position < 10) {
            convertView = mInflater.inflate(R.layout.standard, null);           
        } else {
            convertView = mInflater.inflate(R.layout.custom, null);
            Log.i(TAG, "Inflated custom layout");
        }
        Log.i(TAG, "getView, position = "+position);
        holder = new ViewHolder();
        holder.tv = (TextView) convertView.findViewById(R.id.tv);
        holder.radioButton = (RadioButton) convertView.findViewById(R.id.radioButton1);             
        convertView.setTag(holder);             
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    ...
    Log.i(TAG, "getView() : position = " + position);
}

Here's what I see in the logcat:

getView, position = 0
getView, position = 1
getView, position = 2

The rest of the getView logs do NOT show; I would assume that this line would appear all the way up to getView, position = 10.

And this line: convertView = mInflater.inflate(R.layout.custom, null); is never called, because Inflated custom layout doesn't show up in the logcat.

It's funny though, because the bottom logcat is called always (as I scroll down the ListView):

getView() : position = 0
getView() : position = 1
getView() : position = 2
getView() : position = 3
getView() : position = 4
getView() : position = 5
getView() : position = 6
getView() : position = 7
getView() : position = 8
getView() : position = 9
getView() : position = 10

Why is my custom layout not inflated?

gosr
  • 4,593
  • 9
  • 46
  • 82

2 Answers2

1

You need to inflate the layout when the getView() methods View convertView is not null. The View which is returned from getView() will be reused. If you check the view is null and inflate it will inflate only when its first time returning the View.

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {              
        if (position < 10) {
            convertView = mInflater.inflate(R.layout.standard, null);           
        }
        Log.i(TAG, "getView, position = "+position);
        holder = new ViewHolder();
        holder.tv = (TextView) convertView.findViewById(R.id.tv);
        holder.radioButton = (RadioButton) convertView.findViewById(R.id.radioButton1);             
        convertView.setTag(holder);             
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    ...

    if(position > 10) {
         convertView = mInflater.inflate(R.layout.custom, null);
         Log.i(TAG, "Inflated custom layout");
    }

    Log.i(TAG, "getView() : position = " + position);
}
Lalith B
  • 11,843
  • 6
  • 29
  • 47
  • But in this case every time when position goes above 10 custom view will be inflated right ? Suppose if the getView method call for 100 times with position as 11 then 100 instance of that view will be created.. – Triode Apr 19 '13 at 07:14
  • yes that is the requirement. the listview will call Runtime.gc() every time the listitems are out of the visibleView. – Lalith B Apr 22 '13 at 04:27
0

Inflate custom layout outside convertView == null. Since, convertView will usually return a previously recycled view and hence, your custom view never gets inflated.

Gaurav Arora
  • 17,124
  • 5
  • 33
  • 44