Constraints of multiple layouts listview for android

Programming listview with multiple layout is very easy with android built in method. It is very useful but be careful with the constraints. I will like to introduce about the constraints of list adapter with multiple view types.

 

(1) getItemViewType(int position) method

You must override getItemViewType. Let’s say you have two type of layouts, seperator and text layouts, you have to returned in this way.

@Override
public int getItemViewType(int position) {
    if(mList.get(position).type == TYPE_SEPERATOR){
        return 0; // seperator layout
    }
    return 1;  // text layout
} 

 

(2) getItemViewType index begin with 0

This constraint have stated in android developers reference.

So the index returned at getItemViewType must be begin with 0 instead of 1.

(3) getViewTypeCount() method

You must override getViewTypeCount() method. From example above, you have two different type of layouts, so the

(4) Using multiple ViewHolder class

If you are doing somethings as below, you apps will crash due to NULL EXCEPTION.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
	
	int rowType = getItemViewType(position);
	
	TextView seperator = null;
	CheckBox itemCheckbox = null;
	
	if(convertView == null){
		
		ViewHolder holder = new ViewHolder();
		
		if(rowType == PreferencesItem.TYPE_SEPERATOR){				
			// row title
			convertView = LayoutInflater.from(mContext).inflate(R.layout.preferences_header, parent,false);
			
			seperator = (TextView)convertView.findViewById(R.id.textSeparator);
			
		}else{
			// row item
			convertView = LayoutInflater.from(mContext).inflate(R.layout.preferences_content, parent,false);
			
			itemCheckbox = (CheckBox)convertView.findViewById(R.id.itemCheckbox);
			
		}
		
		holder.itemCheckbox = itemCheckbox;
		holder.seperator = seperator;
		
		convertView.setTag(holder);
		
	}else{
		
		ViewHolder holder = (ViewHolder)convertView.getTag();
		
		if(rowType == PreferencesItem.TYPE_SEPERATOR){				
			// row title					
			seperator = (TextView)holder.seperator;				
		}else{
			// row item
			itemCheckbox = (CheckBox)holder.itemCheckbox;
		}			
	}
	
	return convertView
}

public static class ViewHolder {
	public TextView textView;
	public CheckBox checkBox;
}

You unable to handle the convertView, so you might get null’s checkbox or null’s textview from ViewHolder passed by convertView.getTag().

Solution of this problems is create multiple ViewHolder class. Let’s say, in this tutorial, you must prepare ViewHolderForCheckbox and ViewHolderForSeperator. It should looks like this.

public static class ViewHolderForCheckbox {
	public CheckBox checkBox;
}
public static class ViewHolderForSeperator{
	public TextView seperator;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
	
	int rowType = getItemViewType(position);
	
	TextView seperator = null;
	CheckBox itemCheckbox = null;
	
	if(convertView == null){
		
		if(rowType == PreferencesItem.TYPE_SEPERATOR){				
			// row title
			convertView = LayoutInflater.from(mContext).inflate(R.layout.preferences_header, parent,false);
			
			seperator = (TextView)convertView.findViewById(R.id.textSeparator);
			
			ViewHolderForSeperator holder = new ViewHolderForSeperator();
			holder.seperator = seperator;
			convertView.setTag(holder);
		}else{
			// row item
			convertView = LayoutInflater.from(mContext).inflate(R.layout.preferences_content, parent,false);
			
			itemCheckbox = (CheckBox)convertView.findViewById(R.id.itemCheckbox);
			
			ViewHolderForCheckbox holder = new ViewHolderForCheckbox();
			holder.itemCheckbox = itemCheckbox;
			convertView.setTag(holder);
		}			
		
	}else{
		
		
		
		if(rowType == PreferencesItem.TYPE_SEPERATOR){				
			// row title
			ViewHolderForSeperator holder = (ViewHolderForSeperator)convertView.getTag();
			seperator = (TextView)holder.seperator;				
		}else{
			// row item
			ViewHolderForCheckbox holder = (ViewHolderForCheckbox)convertView.getTag();
			itemCheckbox = (CheckBox)holder.itemCheckbox;
		}			
	}
	
	return convertView
}

Advertisements

One thought on “Constraints of multiple layouts listview for android

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s