自己新建一个类: public class VoicegameAdapter extends ArrayAdapter<String> { String []words;//不仅限字符串,也可以是自定义一个类里面的一组数据 Context context; private int resourceId; public VoicegameAdapter(Context context, int resource, String []word) { super(context, resource, word); this.context=context; this.words=word; resourceId=resource; } @NonNull @Override public View getView(int position, View convertView, ViewGroup parent) { String word=words[position]; final View mView ; //读取layout文件,需要多种样式时,可以通过判断word中包含的数据再分别绑定对应的layout mView = LayoutInflater.from(getContext()).inflate(resourceId, null); final TextView voicegame_word= (TextView) mView.findViewById(R.id.voicegame_word); voicegame_word.setText(word); return mView; } }
android 为listview设置自定义adapter
最新推荐文章于 2023-03-27 09:21:43 发布