ListView带有显示图像和文本的自定义适配器

时间:2017-05-22 13:31:30

标签: android listview

我想要一个显示图像和文本的列表视图,所以我按照在线教程进行操作。不幸的是,当我将列表视图的适配器设置为customAdapter时,我的应用程序一直在崩溃。我无法弄清楚崩溃的问题。 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)

我的customAdapter类

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;
    }
}

我的itemrow xml布局

 <?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>

设置适配器

ListView lv = (ListView) findViewById(R.id.lvStudentsList);
customAdapter adapter = new customAdapter(this, R.layout.itemrow, students);
lv.setAdapter(adapter);

3 个答案:

答案 0 :(得分:1)

您的适配器无法添加新视图。

只需使用:

convertView = inflater.inflate(layoutResoruceId, null);

代替:

convertView = inflater.inflate(layoutResoruceId, parent);

答案 1 :(得分:0)

您应该覆盖getCount中的customAdapter方法,以便Android知道它必须在ListView中显示多少内容:

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

另外,请更改此行,因为Adapter不允许添加新视图:

convertView  = inflator.inflate(layoutResoruceId, parent);

convertView  = inflator.inflate(layoutResoruceId, null);

答案 2 :(得分:0)

显然,问题出在这一行(在customAdapter.java中):

convertView = inflater.inflate(layoutResoruceId, parent);

它应该是:

convertView = inflater.inflate(layoutResoruceId, null);

我真的不知道为什么,但如果有人能给出某种解释我会很感激。感谢。