Android : ListView + BaseAdapter-简单应用

 ​​容器与适配器:​​​​​        http://t.csdnimg.cn/ZfAJ7

示例图:

Android : ListView + BaseAdapter-简单应用_第1张图片

实体类 News.java

package com.example.mylistviewbaseadapter.entity;


public class News {
    private   String title;
    private  String content;
    private int img;

    public News(String title,String content, int img){
        this.title = title;
        this.content = content;
        this.img =img;
    }

    public String getTitile() {
        return title;
    }

    public void setTitile(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

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

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }
}

适配器 写个类继承BaseAdapter 

package com.example.mylistviewbaseadapter;

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 com.example.mylistviewbaseadapter.entity.News;

import java.util.List;

public class NewsAdpater extends BaseAdapter {
    private List date;
    private Context context;
    //構造器
    public NewsAdpater(List date,Context context){
        this.date = date;
        this.context = context;
    }

    //BaseAdapter最基本的几个方法:
    // 1. getCount 填充的数据集数
    // 2.getItem 数据集中指定索引对应的数据项
    // 3. getItemId 指定行所对应的ID
    // 4. getView 每个Item所显示的类容

    @Override
    public int getCount() {
        //充的数据集数
        return date.size();
    }

    @Override
    public Object getItem(int position) {
        //数据集中指定索引对应的数据项
        return date.get(position);
    }

    @Override
    public long getItemId(int position) {
        // 指定行所对应的ID
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //每个Item所显示的类容

        convertView =  LayoutInflater.from(context).inflate(R.layout.list_test,parent,false);
        ImageView imageView = convertView.findViewById(R.id.btnImg);
        TextView tvTitle = convertView.findViewById(R.id.tvH);
        TextView tbCont = convertView.findViewById(R.id.tvCont);

        //摄入值
        imageView.setBackgroundResource(date.get(position).getImg());
        tvTitle.setText(date.get(position).getTitile());
        tbCont.setText(date.get(position).getContent());
        return convertView;
    }
/**
     //优化
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoler holer = null;
        if(holer == null){
            holer = new ViewHoler();
            //每个Item所显示的类容

            convertView =  LayoutInflater.from(context).inflate(R.layout.list_test,parent,false);
            holer.imageView = convertView.findViewById(R.id.btnImg);
            holer.tvTitle = convertView.findViewById(R.id.tvH);
            holer.tbCont = convertView.findViewById(R.id.tvCont);

            convertView.setTag(holer);

        }else{
            holer = (ViewHoler)convertView.getTag();
        }

        //摄入值
        holer.imageView.setBackgroundResource(date.get(position).getImg());
        holer.tvTitle.setText(date.get(position).getTitile());
        holer.tbCont.setText(date.get(position).getContent());
        return convertView;
    }
    static class ViewHoler{
        ImageView imageView;
        TextView tvTitle;
        TextView tbCont;
    }
*/
}

MainActivity.java

package com.example.mylistviewbaseadapter;

import static android.widget.Toast.LENGTH_SHORT;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;


import com.example.mylistviewbaseadapter.entity.News;

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

public class MainActivity extends AppCompatActivity{

   
    private ListView listView;
    private Context context;
    private List listNews =null;

    //适配器
    private  NewsAdpater newsAdpater=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        listView = findViewById(R.id.listVi);

        listNews = new ArrayList<>();
        //传入内容
        for(int i= 0 ; i < 10; i++){
            listNews.add(new News("这是标题"+i,"我是内容我是内容我是内容我是内容我是内容我是内容我是内容"+i,R.mipmap.bg));
        }
        newsAdpater = new NewsAdpater(listNews,context);

          //事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(context, "点击了第" + position + "条数据", LENGTH_SHORT).show();
            }
        });
        //往容器设置适配器
        listView.setAdapter(newsAdpater);
    }

  

}

activity_main.xml




    

list_test.xml



    
    
    
        
        

        
        
    

你可能感兴趣的:(Android,相关,android)