android 为listview设置自定义adapter

来源:互联网 发布:sql have count 编辑:程序博客网 时间:2024/05/16 17:44
自己新建一个类: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;    }}
原创粉丝点击