android 自定义适配器MyAdapter

1.主布局activity_main.xml

xml version="1.0" encoding="utf-8"?>
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.myapplication.MainActivity">

            android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

2.子项布局item

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="6">
            android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/imageView"/>
            android:layout_weight="5"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:weightSum="6">
                    android:id="@+id/userName"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="0dp" />
                    android:id="@+id/content"
            android:layout_weight="4"
            android:layout_width="wrap_content"
            android:layout_height="0dp" />
                    android:id="@+id/time"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="0dp" />
    

3.子项布局属性类Item.java(存放子布局中控件用到的属性值)

package com.example.myapplication;

/**
 * Created by lenovo on 2017-7-24.
 */

public class Item {
    private int imageId;
    private String userName;
    private String content;
    private String time;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

4.自定义适配器(将MainActivity中的数据与子项布局联系起来)

package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2017-7-24.
 */

public class MyAdapter extends BaseAdapter {

    private LayoutInflater mInflater;

    private List list = new ArrayList<>();

    public  MyAdapter(Context context, List list) {
            this.mInflater = LayoutInflater.from(context);
            this.list = list;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        view = mInflater.inflate(R.layout.item, null);
        ImageView imageView = view.findViewById(R.id.imageView);
        TextView userName = view.findViewById(R.id.userName);
        TextView content = view.findViewById(R.id.content);
        TextView time = view.findViewById(R.id.time);

        Item item = list.get(i);
        imageView.setImageResource(item.getImageId());
        userName.setText(item.getUserName());
        content.setText(item.getContent());
        time.setText(item.getTime());

        return view;
    }
}

5.主界面程序

package com.example.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private List list = new ArrayList<>();
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.list);
        for (int i = 0; i < 5; i++) {
            Item item = new Item();
            item.setImageId(R.mipmap.ic_launcher);
            item.setContent(i + "");
            item.setTime(i + "");
            item.setUserName(i + "");
            list.add(item);
        }
        MyAdapter myAdapter = new MyAdapter(MainActivity.this, list);
        listView.setAdapter(myAdapter);
    }
}

6.最终界面

android 自定义适配器MyAdapter_第1张图片


你可能感兴趣的:(android 自定义适配器MyAdapter)