Reputation: 97
I want to have a list view that shows an Image and a text, so I followed an online tutorial. Unfortunately, my app keeps on crashing when I set the list view's adapter to the customAdapter. I couldn't figure out the crashing issue. Logcat:
--------- 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)
My customAdapter class
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;
}
}
My 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>
Setting the adapter
ListView lv = (ListView) findViewById(R.id.lvStudentsList);
customAdapter adapter = new customAdapter(this, R.layout.itemrow, students);
lv.setAdapter(adapter);
Upvotes: 1
Views: 72
Reputation: 21736
Your adapter is unable to add new View.
Just use:
convertView = inflater.inflate(layoutResoruceId, null);
Instead of:
convertView = inflater.inflate(layoutResoruceId, parent);
Upvotes: 1
Reputation: 97
Apparently, the problem was lying in this line (in customAdapter.java):
convertView = inflater.inflate(layoutResoruceId, parent);
it should be:
convertView = inflater.inflate(layoutResoruceId, null);
I really don't know why, but I would appreciate it if someone could give some sort of explanation. Thanks.
Upvotes: 0
Reputation: 4182
You should to override the getCount
method in your customAdapter
in order to Android
know how many itens it have to display in ListView
:
@Override
public int getCount() {
return data.size();
}
Also, change this line, because your Adapter
does not allow adding new view:
convertView = inflator.inflate(layoutResoruceId, parent);
to
convertView = inflator.inflate(layoutResoruceId, null);
Upvotes: 0