Ich möchte eine Listenansicht, die ein Bild und einen Text zeigt, so folgte ich einem Online-Tutorial. Leider stürzt meine App ständig ab, wenn ich den Adapter der Listenansicht auf den customAdapter setze. Ich konnte das Problem nicht lösen. Logcat:ListView mit benutzerdefinierten Adapter, der Bilder und Texte zeigt
--------- beginning of crash
05-22 14:53:29.436 2592-2592/com.example.mohammadel_ghali.attendance E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mohammadel_ghali.attendance, PID: 2592
android.view.InflateException: Binary XML file line #25: addView(View, LayoutParams) is not supported in AdapterView
Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
at com.example.mohammadel_ghali.attendance.customAdapter.getView(customAdapter.java:48)
Meine customAdapter Klasse
public class customAdapter extends ArrayAdapter<Students>{
public Context context;
public int layoutResoruceId;
public List<Students> data = null;
public customAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<Students> objects) {//constructor
super(context, resource, objects);
this.context=context;
this.layoutResoruceId=resource;
this.data=objects;
}
public static class DataHolder{//class for holding the data
ImageView ivPersonHolder;
TextView tvNameHolder;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {//returns the "View"
DataHolder holder = null;
if(convertView==null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(layoutResoruceId, parent);
holder = new DataHolder();
holder.ivPersonHolder = (ImageView)convertView.findViewById(R.id.ivPerson);
holder.tvNameHolder = (TextView)convertView.findViewById(R.id.tvName);
convertView.setTag(holder);
}else{
holder = (DataHolder)convertView.getTag();
}
Students student = data.get(position);
holder.tvNameHolder.setText(student.toString());
holder.ivPersonHolder.setImageResource(student.getImageId());
return convertView;
}
}
Mein itemrow xml Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1">
<ImageView
android:id="@+id/ivPerson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:layout_gravity="center"
android:layout_margin="@dimen/itemmargin"/>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_gravity="center"
android:layout_margin="@dimen/itemmargin"
android:layout_weight="0.33" />
</LinearLayout>
Einstellung der Adapter
ListView lv = (ListView) findViewById(R.id.lvStudentsList);
customAdapter adapter = new customAdapter(this, R.layout.itemrow, students);
lv.setAdapter(adapter);
die stacktrace über ein anderes Problem ist, legen Sie die richtige Ausnahme hier –
ich meine Post bearbeitet, die Ausnahme besagt. –