gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:32:11 26 4
gpt4 key购买 nike

我想要一个显示图像和文本的 ListView ,所以我遵循了在线教程。不幸的是,当我将 ListView 的适配器设置为 customAdapter 时,我的应用程序一直在崩溃。我无法弄清楚崩溃的问题。日志:

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

我的自定义适配器类

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

我的项目行 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);

最佳答案

您的适配器无法添加新 View 。

只需使用:

convertView = inflater.inflate(layoutResoruceId, null);

代替:

convertView = inflater.inflate(layoutResoruceId, parent);

关于android - 带有显示图像和文本的自定义适配器的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44114240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com