AdapterView(ListView)

AdapterView(Image View用int类型)

1.先创建一个实体类

    public class Fruit {
    private String name;
    //注意图片为int类型
    private int imageId;
    private String str;
    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public Fruit(String name, int imageId) {

        this.name = name;
        this.imageId = imageId;
    }

    public Fruit(String name, int imageId, String str) {

        this.name = name;
        this.imageId = imageId;
        this.str = str;
    }

    public int getImageId() {
        return imageId;
    }

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

    public Fruit(String name) {

        this.name = name;
    }

    public String getName() {
        return name;
    }

2.在创建布局

//为自定义布局文件
    <?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="match_parent"
     >
      <ImageView 
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />
    <TextView 
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginLeft="10dip" />


</LinearLayout>

3.自定义适配器

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

import android.content.Context;
import android.support.v4.widget.ViewDragHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FruitAdapter extends ArrayAdapter<Fruit> {
    private int resourcedId;
    public FruitAdapter(Context context,int textViewResourceId,List<Fruit> objecs){
        super(context,textViewResourceId,objecs);
        resourcedId = textViewResourceId;

    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;

        Fruit fruit = getItem(position);
        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourcedId, null);
        }else{
            view=convertView;
        }
        //得到自定义布局中的控件,为其赋值
        ImageView fruitImage=(ImageView) view.findViewById(R.id.fruit_image);
        TextView fruitView = (TextView) view.findViewById(R.id.fruit_name);
        fruitImage.setImageResource(fruit.getImageId());
        fruitView.setText(fruit.getName());
        return view;
    }

}

优化

    public class FruitAdapter extends ArrayAdapter<Fruit> {
    private int resourcedId;

    public FruitAdapter(Context context,int textViewResourceId,List<Fruit> objecs){
        super(context,textViewResourceId,objecs);
        resourcedId = textViewResourceId;

    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        //ViewHolder viewHolder;
        Fruit fruit = getItem(position);
        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourcedId, null);
            viewHolder = new ViewHolder();
            viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
            viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name);
            view.setTag(viewHolder);

        }else{
            view=convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        //得到自定义布局中的控件,为其赋值
        viewHolder.fruitImage.setImageResource(fruit.getImageId());
        viewHolder.fruitName.setText(Fruit.getName());
        return view;
    }
    "重点"
    //创建自己的缓存类
    class ViewHolder{
    ImageView fruitImage;
    TextView fruitName;

    }

}   

4.主程序

    public class MainActivity extends Activity {
    private List<Fruit> fruitList=new ArrayList<Fruit>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //main布局中只有listView,此博客中没有写这个布局
        setContentView(R.layout.activity_main);
        initFruits();
        //传入的为自定义布局
        FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
        //得到布局中的listView控件
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        //给每条添加点击事件,注意为Item点击事件
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //用position得到每条记录
                Fruit fruit = fruitList.get(position);
                Toast.makeText(MainActivity.this, fruit.getStr(), Toast.LENGTH_SHORT).show();

            }
        });
    }
//初始化list中的数据
    private void initFruits(){
        Fruit apple =new Fruit("apple",R.drawable.a,"你好");
        fruitList.add(apple);
        Fruit banana = new Fruit("banana",R.drawable.e,"你好,你吃饱了没?");
        fruitList.add(banana);
        Fruit orange = new Fruit("orange",R.drawable.f,"你猜,我吃了没?");
        fruitList.add(orange);
        Fruit watermelon= new Fruit("watermelon",R.drawable.fg,"猜你妹啊,找打是吧!");
        fruitList.add(watermelon);
        Fruit pear=new Fruit("pear",R.drawable.g,"嗯哼,要动手");
        fruitList.add(pear);
        Fruit grape = new Fruit("grape",R.drawable.i1,"动手咋地");
        fruitList.add(grape);
        Fruit pineapple = new Fruit("pineapple",R.drawable.i2,"咋地,打残你");
        fruitList.add(pineapple);
        Fruit strawberry = new Fruit("strawberry",R.drawable.ic_launcher,"呦呦,好怕");
        fruitList.add(strawberry);
        Fruit cherry = new Fruit("Cherry",R.drawable.m,"怕就好");
        fruitList.add(cherry);
        Fruit mango= new Fruit("mango",R.drawable.r,"....");
        fruitList.add(mango);
        Fruit pineapple1 = new Fruit("pineapple1",R.drawable.v,"脑残");
        fruitList.add(pineapple1);
        Fruit strawberry1 = new Fruit("strawberry1",R.drawable.w,"脑残你妹");
        fruitList.add(strawberry1);
        Fruit cherry1 = new Fruit("Cherry1",R.drawable.ic_launcher,"....");
        fruitList.add(cherry1);
        Fruit mango1= new Fruit("mango1",R.drawable.ic_launcher,"咋地,打残你");
        fruitList.add(mango1);


    }
}

5.使用

 setContentView(R.layout.list_layout);
         init();
         //只是使用一个list布局的框架
        mListView_view = (ListView) findViewById(R.id.list_view);
        SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.simple_layout,new String[]{"name","pass"},new int[]{R.id.name,R.id.pass});
        mListView_view.setAdapter(adapter);

BaseAdapter

1.创建listView(list_view.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="match_parent">
    <ListView
        android:id="@+id/list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

2.创建要添加到list上的布局(student.xml)

单条student的布局

    <?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="match_parent">
<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

创建Student类

    package com.test.dukang.baseview;
//写出set()和get()方法

public class Student {
    private String age;
    private String sex;
    private String name;

    public Student(String age, String sex, String name) {
        this.age = age;
        this.sex = sex;
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

4.创建StudentAdapter

    package com.test.dukang.baseview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2015/8/24.
 */
public class StudentAdapter extends BaseAdapter {
    private List<Student> list;
    private LayoutInflater inflater;
    //通过构造器传入所需要传递的值
    public StudentAdapter(List<Student> list, LayoutInflater inflater) {
        this.list = list;
        this.inflater = inflater;
    }

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

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //加载单个student的布局文件
        View view=inflater.inflate(R.layout.student,null);
        //得到单个student的指针
        Student student=list.get(position);
        //得到布局中的控件,注意用的view.find
        TextView name= (TextView) view.findViewById(R.id.name);
        TextView sex= (TextView) view.findViewById(R.id.sex);
        TextView age= (TextView) view.findViewById(R.id.age);
        //给控件设置相应的值
        name.setText(student.getName());
        sex.setText(student.getSex());
        age.setText(student.getAge());
        return view;
    }
}

5.创建主类(在主类中创建List)

    package com.test.dukang.baseview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

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

public class MainActivity extends AppCompatActivity {
    private List<Student> list;
    private LayoutInflater inflater;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //注意加载list_view布局文件
        setContentView(R.layout.list_view);
        //初始化
        init();
      "得到布局填充,相当于上面的from(getContext()),用于得到上下文"
        inflater=getLayoutInflater();
        StudentAdapter adapter=new StudentAdapter(list,inflater);
        //找到list_view
        ListView listView=(ListView)findViewById(R.id.list_view);
        listView.setAdapter(adapter);
    }
    public void init(){
        //在使用类中创建list
        list=new ArrayList<>();
        Student xiaoming=new Student("18","男","小明");
        Student xiaozhao=new Student("18","男","小LI");
        Student xiaoli=new Student("18","男","小HONG");
        list.add(xiaoming);
        list.add(xiaozhao);
        list.add(xiaoli);
    }


}

SimpleAdapter

        private ListView mListView_view;
   //创建List对象
    private List<HashMap<String,String>> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        "***导入只含有listView布局的xml文件***"
        setContentView(R.layout.list_layout);
         init();
        //从布局文件中的到相应的控件id
        mListView_view = (ListView) findViewById(R.id.list_view);
        //这次导入的为自己定义的布局文件
        final SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.simple_layout,new String[]{"name","pass"},new int[]{R.id.name,R.id.pass});
        mListView_view.setAdapter(adapter);
        mListView_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //得到其中的单条数据
                HashMap<String,String> item=list.get(position);

              Log.d("date"," "+item.get("name"));
            }
        });
}

//初始化
public void init(){
        "在主文件中创建List,然后为list添加对象"
        list=new ArrayList<>();
        HashMap<String,String> zhansan=creatHashmap("战三","123");
        list.add(zhansan);
        HashMap<String,String> lisi=creatHashmap("李四","123");
        list.add( lisi);
        HashMap<String,String> wangwu=creatHashmap("王五","123");
        list.add(wangwu);
        HashMap<String,String> zhaoliu=creatHashmap("赵六","123");
        list.add(zhaoliu);

    }   
            "因为传入的为hash表的形式"
 private  HashMap<String,String> creatHashmap (String name,String pass){
        HashMap<String,String> zhansan =new HashMap<>();
        zhansan.put("name",name);
        zhansan.put("pass",pass);
        return zhansan;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值