Android中LIstView组件和适配器的联合使用——Android studio

MainActivity7

package com.example.myapplication_one;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;


public class MainActivity7 extends AppCompatActivity {

    ListView listview1;
    private String[] data = {"济南","淄博","威海"};
    List<Student> list = new ArrayList<Student>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //用于加载页面布局   R.layout.名称
        setContentView(R.layout.layout6);
        init();
        initStudent();//初始化学生类


        /*
         * Adapter:桥梁的作用,连接控件与数据的桥梁
         * ArrayAdapter:数组适配器
         * context:上下文
         * resource:每一个Item显示的样式
         * objects:需要绑定的数据源
         */
        //用于连接简单的数据源与控件
      /*  ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity7.this,
                android.R.layout.simple_list_item_1,//简单的显示一个textview样式
                data);   */
        MyAdapter myAdapter = new MyAdapter(MainActivity7.this,R.layout.layout7,list);
        //控件绑定适配器
        listview1.setAdapter(myAdapter);
    }

    //初始化学生数据
    private void initStudent(){
        for(int i=0; i<3 ;i++){
            Student student1 = new Student(R.drawable.a1,"2019001");
            list.add(student1);
            Student student2 = new Student(R.drawable.a1,"2019002");
            list.add(student2);
            Student student3 = new Student(R.drawable.a1,"2019003");
            list.add(student3);
        }
    }

    private void init() {

        listview1 = findViewById(R.id.list_1);
    }


}

layout6

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我爱你,中国🇨🇳" />


    <ListView
        android:id="@+id/list_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</RelativeLayout>

layout7

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


    <ImageView
        android:id="@+id/img_1"
        android:layout_width="50px"
        android:layout_height="wrap_content"
         />

    <TextView
        android:id="@+id/textView_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />

</LinearLayout>

MyAdapter.java

package com.example.myapplication_one;

import android.content.Context;
import android.view.*;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends ArrayAdapter<Student> {

    private int resourceId;

    public MyAdapter(Context context, int resource , List<Student> list) {
        super(context, resource,list);
        resourceId = resource;
    }


    @Override
    public View getView(int position,  View convertView,  ViewGroup parent) {
        Student student = getItem(position);

        //java在这个地方不能通过findbyid()方法,使用inflate方法找到Item的页面布局
        /*
         * inflate(resourceId,parent,false);
         * 第三个参数:表示只让在父类容器中声明的layout属性有效,
         * 但不会为这个View添加父类布局
         * 因为一旦view有了父类布局,就无法添加到listView中了。
         */
        View view = LayoutInflater.from(getContext())//getContext()获取上下文
                .inflate(resourceId,parent,false);//parent:父类容器,在这里当作根结点
        ImageView imageView =
                (ImageView) view.findViewById(R.id.img_1);
        TextView textView =
                (TextView) view.findViewById(R.id.textView_1);

        imageView.setImageResource(student.getImgId());
        textView.setText(student.getNum());

        return view;
    }
}

Student.java

package com.example.myapplication_one;

public class Student {
    private int imgId;
    private String num;

    public  Student(int imgId,String num){
        this.imgId = imgId;
        this.num = num;
    };

    public int getImgId() {
        return imgId;
    }

    public void setImgId(int imgId) {
        this.imgId = imgId;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱睡觉的小馨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值