码迷,mamicode.com
首页 > 移动开发 > 详细

Android——列表视图 ListView(三)BaseAdapter

时间:2016-04-07 20:49:33      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

activity_activitybase.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lv_base"
    >


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

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/anniu5"
        android:layout_gravity="center_vertical"
        android:id="@+id/iv_1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="20dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="生命可贵"
            android:textSize="20dp"
            android:id="@+id/tv_1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="安全出口"
            android:textSize="20dp"
            android:id="@+id/tv_2"/>


    </LinearLayout>


</LinearLayout>

biaozhi类

Biaozhi.java
package com.example.chenshuai.test321;


/**
 * Created by chenshuai on 2016/4/6.
 */
//实体类 只负责装数据
public class Biaozhi {

    private int image;
    private String name;
    private  String content;

    public int getImage() {
        return image;
    }

    public String getName() {
        return name;
    }

    public String getContent() {
        return content;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public void setName(String name) {
        this.name = name;
    }

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

    public Biaozhi(int image, String name, String content) {
        this.image = image;
        this.name = name;
        this.content = content;
    }

    public Biaozhi()
    {

    }

    @Override
    public String toString() {
        return "Biaozhi{" +
                "image=" + image +
                ", name=‘" + name + ‘\‘‘ +
                ", content=‘" + content + ‘\‘‘ +
                ‘}‘;
    }

}

Activitybase.java

数据源:一种为List集合,另一种为biaozhi类

package com.example.chenshuai.test321;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

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

public class Activitybase extends AppCompatActivity {

    //数据源 成员变量
    //List<Map<String,Object>> im;

    List<Biaozhi> lf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activitybase);

        ListView base_1 = (ListView)findViewById(R.id.lv_base);

        //准备数据源

        lf = new ArrayList<Biaozhi>();
        lf.add(new Biaozhi(R.drawable.anniu3,"内容1","出口1"));
        lf.add(new Biaozhi(R.drawable.anniu4,"内容2","出口2"));
        lf.add(new Biaozhi(R.drawable.anniu5,"内容3","出口3"));
        lf.add(new Biaozhi(R.drawable.anniu6,"内容1","出口1"));
        lf.add(new Biaozhi(R.drawable.anniu7,"内容2","出口2"));
        lf.add(new Biaozhi(R.drawable.anniu8,"内容3","出口3"));
        lf.add(new Biaozhi(R.drawable.anniu9,"内容1","出口1"));
        lf.add(new Biaozhi(R.drawable.anniu10,"内容2","出口2"));
        lf.add(new Biaozhi(R.drawable.anniu11,"内容3","出口3"));
        lf.add(new Biaozhi(R.drawable.anniu9,"内容1","出口1"));
        lf.add(new Biaozhi(R.drawable.anniu10,"内容2","出口2"));
        lf.add(new Biaozhi(R.drawable.anniu11,"内容3","出口3"));
      /*  im = new ArrayList<Map<String,Object>>();

        Map<String,Object> map = new HashMap<String,Object>();

        map.put("image",R.drawable.anniu3);
        map.put("name","安全出口1");
        map.put("content","保护生命1...");
        im.add(map);

        map = new HashMap<String,Object>();
        map.put("image",R.drawable.anniu4);
        map.put("name","安全出口2...");
        map.put("content", "保护生命2");
        im.add(map);

        map = new HashMap<String,Object>();
        map.put("image",R.drawable.anniu5);
        map.put("name","安全出口3....");
        map.put("content", "保护生命3");
        im.add(map);

        map = new HashMap<String,Object>();
        map.put("image",R.drawable.anniu6);
        map.put("name","安全出口4");
        map.put("content", "保护生命4");
        im.add(map);

        map = new HashMap<String,Object>();
        map.put("image",R.drawable.anniu7);
        map.put("name","安全出口5");
        map.put("content", "保护生命5");
        im.add(map);

        map = new HashMap<String,Object>();
        map.put("image",R.drawable.anniu8);
        map.put("name","安全出口6");
        map.put("content", "保护生命6");
        im.add(map);

        map = new HashMap<String,Object>();
        map.put("image",R.drawable.anniu9);
        map.put("name","安全出口7");
        map.put("content", "保护生命7");
        im.add(map);

        map = new HashMap<String,Object>();
        map.put("image",R.drawable.anniu10);
        map.put("name","安全出口8");
        map.put("content", "保护生命8");
        im.add(map);

        map = new HashMap<String,Object>();
        map.put("image",R.drawable.anniu9);
        map.put("name","安全出口7");
        map.put("content", "保护生命7");
        im.add(map);

        map = new HashMap<String,Object>();
        map.put("image",R.drawable.anniu10);
        map.put("name","安全出口8");
        map.put("content", "保护生命8");
        im.add(map);*/

        base_1.setAdapter(new mybaseadapter());
    }

    //抽象类 要继承并实现抽象方法后才能使用
    class mybaseadapter extends BaseAdapter{

        //获取要显示的行数
        @Override
        public int getCount() {
            //return im.size();
            Log.e("Tag", "size= " +lf.size() );
            return lf.size();
   }

        //获取对应下标的数据
        @Override
        public Object getItem(int position) {
            //return im.get(position);
            return lf.get(position);
        }

        //获取对应数据的id
        @Override
        public long getItemId(int position) {
            return position;
        }

        //通过getView来实现Listview
        //给视图适配数据返回view视图
        //position 数据的下标(第几个视图)
        //convertView 可重复使用的视图
        //parent 父视图
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            //如果convertView为null,就加载layout文件
            if (convertView == null) {
                Log.e("Tag", "position= " + position);

                //1.获取layout文件  用加载器

                LayoutInflater layoutInflater = getLayoutInflater();

                convertView = layoutInflater.inflate(R.layout.simple_layout, null);
            }

            //2.把数据放入layout文件中  数据和layout文件的视图组件进行绑定

            //(1)从数据源里获取一行数据
            //Map<String,Object>map = im.get(position);

            Biaozhi biaozhi = lf.get(position);

            //(2)获取layout文件的视图组件
            ImageView iv_1 = (ImageView)convertView.findViewById(R.id.iv_1);
            TextView tv_1 = (TextView)convertView.findViewById(R.id.tv_1);
            TextView tv_2 = (TextView)convertView.findViewById(R.id.tv_2);

            //(3)绑定  数据适配
            /*iv_1.setImageResource((int)map.get("image"));
            tv_1.setText(map.get("name").toString());
            tv_2.setText(map.get("content").toString());
*/
            iv_1.setImageResource(biaozhi.getImage());
            tv_1.setText(biaozhi.getName());
            tv_2.setText(biaozhi.getContent());
            //3.返回视图
            return convertView;
        }
    }
}

技术分享

 

Android——列表视图 ListView(三)BaseAdapter

标签:

原文地址:http://www.cnblogs.com/Chenshuai7/p/5365096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!