Android ListView 自定义 Adapter

时间:2023-03-08 16:00:51

自定义Adapter类

public class ListViewAdapter extends BaseAdapter {
    private static final String TAG = MainActivity.class.getName();

    private Context context; // 运行上下文
    private List<Map<String, Object>> listItems; // 商品信息集合
    private LayoutInflater listContainer; // 视图容器

    public final class ListItemView { // 自定义控件集合
        public ImageView image_status;
        public TextView text_storage;
        public TextView text_device;
        public TextView text_temperature;
        public TextView text_humidity;
        public TextView text_updatetime;
    }

    public ListViewAdapter(Context context, List<Map<String, Object>> listItems) {
        this.context = context;
        this.listContainer = LayoutInflater.from(this.context); // 创建视图容器并设置上下文
        this.listItems = listItems;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        Log.i(TAG, "getCount:" + listItems.size());
        return listItems.size();//获取数量
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Log.i(TAG, "getView");
        // 自定义视图
        ListItemView listItemView = null;
        if (convertView == null) {
            listItemView = new ListItemView();
            // 获取list_item布局文件的视图
            convertView = listContainer.inflate(R.layout.list_item, null);
            listItemView.image_status = (ImageView) convertView
                    .findViewById(R.id.item_image_status);
            listItemView.text_storage = (TextView) convertView
                    .findViewById(R.id.item_text_storage);
            listItemView.text_device = (TextView) convertView
                    .findViewById(R.id.item_text_device);
            listItemView.text_temperature = (TextView) convertView
                    .findViewById(R.id.item_text_temperature);
            listItemView.text_humidity = (TextView) convertView
                    .findViewById(R.id.item_text_humidity);
            listItemView.text_updatetime = (TextView) convertView
                    .findViewById(R.id.item_text_updatetime);

            // 设置文字和图片
            listItemView.image_status.setBackgroundResource((Integer) listItems
                    .get(position).get("image"));
            listItemView.text_storage.setText((String) listItems.get(position)
                    .get("storage"));
            listItemView.text_device.setText((String) listItems.get(position)
                    .get("device"));
            listItemView.text_temperature.setText((String) listItems.get(
                    position).get("temperature"));
            listItemView.text_humidity.setText((String) listItems.get(position)
                    .get("humidity"));
            listItemView.text_updatetime.setText((String) listItems.get(
                    position).get("updatetime"));

            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }
        return convertView;
    }

}

自定义布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/item_image_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/item_text_storage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/item_text_device"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/item_text_temperature"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/item_text_humidity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/item_text_updatetime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

调用自定义Adapter方法

    private void initListView() {
        data_listview = (ListView) findViewById(R.id.data_listview);
        listItems = getListItems();
        listadapter = new ListViewAdapter(this, listItems);
        Log.i(TAG, "setAdapter");
        data_listview.setAdapter(listadapter);
    }

    private List<Map<String, Object>> getListItems() {
        List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < 10; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", R.drawable.ic_logo);
            map.put("storage", "仓库");
            map.put("device", "设备");
            map.put("temperature", "温度");
            map.put("humidity", "湿度");
            map.put("updatetime", "上传时间");
            listItems.add(map);
        }
        return listItems;
    }