I’m inflating two layout in one listview. The data are in json format. What I do is I concat the json together, and check the appropriate position for each layout (the first layout only appear from position 0 to first-json’s length – 1).
The problem arise when the list view get longer, and I can scroll down through it. It came out with a Null Pointer exception. So I commented some of the code, and the error disappear. But what I got is not really what I’m expecting:
It interchange the format randomly. (Assume the length of the first json is x, so 0 to x-1 should be the first layout. But when I scroll up & down, sometimes somewhere between 0 and x changed to second layout. As well as other row > x, sometimes changed to first layout)
Here’s the code of the adapter
public class CustomAdapter extends BaseAdapter {
private Activity activity;
private JSONArray data;
private static LayoutInflater inflater=null;
private SecondItem second_item;
private FirstItem first_item;
private int firstLength;
private boolean hasFirst = false;
public CustomAdapter(Activity a, JSONArray firstArray, JSONArray secondArray) {
activity = a;
first_item = new FirstItem (firstArray);
second_item = new SecondItem (secondArray);
firstLength = first_item.getLength();
if (!firstArray.isNull(0)) {
data=CustomUtils.concatJsonArray(firstArray, secondArray);
hasFirst = true;
}
else
data = secondArray;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return data.length();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public static class ViewHolder{
public TextView txt_both;
public TextView txt_first_only;
...
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
ViewHolder holder;
if(convertView==null){
if (position < firstLength && hasFirst) {
vi = inflater.inflate(R.layout.first_item, null);
}
else {
vi = inflater.inflate(R.layout.second_item, null);
}
holder=new ViewHolder();
holder.txt_both=(TextView)vi.findViewById(R.id.txt_both);
...
if (position < firstLength && hasFirst) {
holder.txt_firstOnly=(TextView)vi.findViewById(R.id.txt_firstOnly);
...
}
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
if (position < firstLength && hasFirst) {
holder.txt_both.setText(first_item.getContent(position));
...
here is the commented block, which causes null pointer exception before (The error point the line holder.txt_firstOnly.setText) :
/*if (first_item.getStatus(position)==0) {
holder.txt_firstOnly.setText(...);
}
else if (first_item.getStatus(position)==1) {
holder.txt_firstOnly.setText(...);
}
else if (first_item.getStatus(position)==2) {
holder.txt_firstOnly.setText(...);
} */
and here’s the rest of the code :
}
else {
holder.txt_both.setText(second_item.getContent(position - firstLength));
...
}
return vi;
}
}
Your custom adapter should override the methods
getViewTypeCount()andgetItemViewType (int position). Right now, the OS thinks all the views have the same view type, so it’s arbitrarily passing in a previously created view. As you’ve seen, sometimes this isn’t the right view for the position.