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