Hi unten ist meine Klasse für Baseadapter, aber es funktioniert nicht !!Listview mit mehrreihigem Layout
ich versuche Listview mit mehreren Reihen zu verwenden, verstehe ich nicht, wo ich falsch war erste Layout enthält nur Textview und der zweite enthält Textview und 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 ;
}}
ich habe einen Fehler
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);
Anstelle von 'convertView = View.inflate (Kontext, R.layout.item_sliding_menu, null) In beiden Fällen verwenden Sie convertView = layoutInflater.inflate (R.layout.yourLayout, parent, false); Hoffe du weißt, wie man den layoutInflater bekommt. –