0

I have a listview with different rows with different layout.

layout1.xml

<LinearLayout
android:height="wrap_content"
android:width="fill_parent">

<TextView
android:height="wrap_content"
android:width="wrap_content"
android:text="One" 
android="@+id/one"/>

</LinearLayout>

layout2.xml

 <LinearLayout
    android:height="wrap_content"
    android:width="fill_parent">

    <TextView
    android:height="wrap_content"
    android:width="wrap_content"
    android:text="One"
    android="@+id/one" />

 <TextView
    android:height="wrap_content"
    android:width="wrap_content"
    android:text="Two"
    android="@+id/one" />

    </LinearLayout>

Since the adapter recycles all of its views, how will it know if layout1 is recycled. If layout1 is recycled, convertView is not null, and I try to find textview2. Should this throw null pointer?

Assume convertView is always recycled and no need to inflate view. So, layout1 is recycled, but the below code is expecting layout2 for the position.

in the getView(),

@Override
  public View getView(int position, View convertView,
                      ViewGroup parent) {
         //A method to tell if layout1 or layout2 should be inflated based on the position
         boolean inflateOne = inflateLayoutOne(position);

   if(convertView == null){


      if(inflateOne)
        convertView = inflater.inflate(R.layout.layout1, null);
      else
        convertView = inflater.inflate(R.layout.layout2, null);
    }

    if(inflateOne){
     TextView tv1 = (TextView)convertView.findViewById(R.id.one);
     tv1.setText("Hello one");
    }
    else{
     TextView tv1 = (TextView)convertView.findViewById(R.id.one);
     tv1.setText("Hello one");
     TextView tv2 = (TextView)convertView.findViewById(R.id.two);
     tv1.setText("Hello Two");.

return convertView;

  }
dcanh121
  • 4,665
  • 11
  • 37
  • 84

3 Answers3

0

To tell which layout the convertView is returning, you should be able to just compare the ids.

if (convertView.getId() == R.layout.layout1){
    //set data for layout 1
}else if(convertView.getId() == R.layout.layout1){
    //set data for layout 1
}
David C Adams
  • 1,953
  • 12
  • 12
  • Exactly, but this is not done on any of the reference articles, or here, http://stackoverflow.com/a/15706073/563306 – dcanh121 Feb 05 '14 at 20:42
0

you are doing it wrong. If you want to have different kind of rows, you have to override getViewTypeCount and getViewItemType. Both return an int value. getView will receive a number of null convertView equals to the value you return in getViewTypeCount. You should use getViewItemType to distinguish between the different view

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

Your adapter must override getItemViewType(int position). From the docs:

Returns: An integer representing the type of View. Two views should share the same type if one can be converted to the other in getView(int, View, ViewGroup). Note: Integers must be in the range 0 to getViewTypeCount() - 1. IGNORE_ITEM_VIEW_TYPE can also be returned.

http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int)

Dave C
  • 928
  • 6
  • 12