Abbes Chouokchou Abbes Chouokchou - 6 months ago 42
Android Question

Listview with multiple row layout

Hi below is my class for baseadapter, but it is not working !!

i try to use ListView with multiple Row, I did not understand where I was wrong
first layout contains just textView and the second contains textview and imageView

public class CustomAdapter extends BaseAdapter {

public static final int TYPE_ODD = 0;
public static final int TYPE_EVEN = 1;




private Context context;
private List<ItemSlideMenu> lstItem;
@Override
public int getViewTypeCount() {
return 2;
}

@Override
public int getItemViewType(int position) {
return lstItem.get(position).getType();
}


public CustomAdapter(Context context, List<ItemSlideMenu> lstItem) {
this.context = context;
this.lstItem = lstItem;
}

@Override
public int getCount() {
return lstItem.size();
}

@Override
public Object getItem(int position) {
return lstItem.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

int listViewItemType = getItemViewType(position);

if (convertView == null) {

if (listViewItemType == TYPE_EVEN) {
convertView = View.inflate(context, R.layout.item_sliding_menu, null);
ImageView img = (ImageView) convertView.findViewById(R.id.item_img);
TextView tv = (TextView) convertView.findViewById(R.id.item_title);

ItemSlideMenu item = lstItem.get(position);
img.setImageResource(item.getImgId());
tv.setText(item.getTitle());

}

else if (listViewItemType == TYPE_ODD) {
convertView = View.inflate(context, R.layout.reseaux, null);
ImageView img = (ImageView) convertView.findViewById(R.id.item_img);
TextView tv = (TextView) convertView.findViewById(R.id.item_title);

ItemSlideMenu item = lstItem.get(position);
img.setImageResource(item.getImgId());
tv.setText(item.getTitle());

}


}

return convertView ;

}}


i have an error

05-27 16:05:45.297 25537-25537/com.example.getgpslocation E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.getgpslocation, PID: 25537
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference


Fragment3.java

listSliding3 = new ArrayList<>();
//Add item for sliding list
listSliding3.add(new ItemSlideMenu("TRAM " , CustomAdapter.TYPE_ODD));
listSliding3.add(new ItemSlideMenu(R.drawable.ic_action_about,"Ligne1",CustomAdapter.TYPE_EVEN ));
listSliding3.add(new ItemSlideMenu(R.drawable.ic_action_about,"Ligne2",CustomAdapter.TYPE_EVEN ));
listSliding3.add(new ItemSlideMenu("BUS " , CustomAdapter.TYPE_ODD));


CustomAdapter customAdapter = new CustomAdapter(getActivity(),listSliding3);
listViewSliding3.setAdapter(customAdapter);

Answer

When adding:

listSliding3.add(new ItemSlideMenu("TRAM " , CustomAdapter.TYPE_ODD));

the "THAM " value is not a resource id, so you will get a NullPointerException on img.setImageResource(item.getImgId());

Use:

listSliding3.add(new ItemSlideMenu("", "TRAM ", CustomAdapter. TYPE_ODD ));

And in adapter do not set image:

    else if (listViewItemType == TYPE_ODD) {
        convertView = View.inflate(context, R.layout.reseaux, null);
        ImageView img = (ImageView) convertView.findViewById(R.id.item_img);
        TextView tv = (TextView) convertView.findViewById(R.id.item_title);

        ItemSlideMenu item = lstItem.get(position);
        tv.setText(item.getTitle());

    }
Comments