4.7

所花时间(包括上课):2

打码量(行):430

博客量(篇):1

了解到知识点:学习BaseAdapter

 

 import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.TextView;

import java.util.ArrayList;

 

public class CustomAdapter extends BaseAdapter {

 

    private ArrayList<String> mData;

    private LayoutInflater mInflater;

 

    public CustomAdapter(Context context, ArrayList<String> data) {

        mData = data;

        mInflater = LayoutInflater.from(context);

    }

 

    @Override

    public int getCount() {

        return mData.size();

    }

 

    @Override

    public Object getItem(int position) {

        return mData.get(position);

    }

 

    @Override

    public long getItemId(int position) {

        return position;

    }

 

    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

 

        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.list_item, parent, false);

            holder = new ViewHolder();

            holder.textView = convertView.findViewById(R.id.textView);

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();

        }

 

        String item = mData.get(position);

        holder.textView.setText(item);

 

        return convertView;

    }

 

    private static class ViewHolder {

        TextView textView;

    }

}

<!-- list_item.xml -->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/textView"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:textSize="18sp"

    android:padding="10dp"

    android:textColor="@android:color/black"/>

posted @ 2024-04-07 20:24  赵千万  阅读(0)  评论(0编辑  收藏  举报