五分钟学会之ListView<simpleAdapter><BaseAdapter>










package com.coderqi.android2lesson_02_simpleadapter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//simpleAdapter是一种基本版的完整的列表视图
public class MainActivity extends AppCompatActivity {
// simpleAdapter可以进行布局文件定制,从而达到自定义的效果

//    simpleAdapter 步骤1:创建一个布局文件,用来给每一个Item(cell)进行布局
//    listview在iOS开发里面叫UITableView
//    iOS里面没有包的概念,有命名空间
//    simpleAdapter 步骤2:
//    在MainActivity布局文件中添加ListView
//    simpleAdapter 步骤三:
//    声明属性并且进行初始化
  private ListView mlistView;

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

//        进行初始化
        mlistView =(ListView) findViewById(R.id.listView);
//        simpleAdapter 步骤4:
//        创建一个simple适配器
//        参数1:是传递的一个上下文对象;
//        参数2:要显示的数据
//        参数3:数据显示的布局文件;
//        参数4:数据提取时用到的关键字;
//        参数5:数据对应额布局文件中的控件ID
        SimpleAdapter adapter =new SimpleAdapter(this,getData(), R.layout. ,new String[]{"image","title","info"},
        new int[]{R.id.image,R.id.title,R.id.info});

//        simpleLeAdapter步骤5:将adapter和listview进行绑定
        mlistView.setAdapter(adapter);
    }
//    在这个方法里面进行数据的创建和初始化,拼装要显示的数据
    private List<Map<String,Object>> getData(){
        List<Map<String,Object>>list =new ArrayList<>();
        Map<String,Object>map =new HashMap<>();
        map.put("image", R.drawable.ic_launcher);
        map.put("title","在水一方");
        map.put("info","伊人却已不在");
        list.add(map);
        Map<String,Object>map1 =new HashMap<>();

        map1.put("image",R.drawable.ic_launcher);
        map1.put("title","核舟记");
        map1.put("info","麻雀虽小");
        list.add(map1);

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

        map2.put("image",R.drawable.ic_launcher);
        map2.put("title","藤蔓");
        map2.put("info","爬墙虎");
        list.add(map2);
        return  list;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5px"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22px"
        android:text="山茶花下"
        />
    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15px"
        android:text="伊人微醺"
        />

</LinearLayout>
</LinearLayout>
******************************************************************************************

BaseAdapter

package com.example.user.android2lesson_day3_baseadapter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

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

import bean.News;

public class MainActivity extends AppCompatActivity {       
//baseAdapter使用
//    1.创建一个布局文件,对Item的控件进行布局
//    2.在对应的Activity中添加ListView控件
//    3.声明属性,并进行初始化
//    4.创建一个类,继承自BaseAdapter
//    5.创建Adapter并进行绑定
    private ListView mlistView;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mlistView = (ListView) findViewById(R.id.listView);

//
//创建Adapter并进行绑定
        MyAdapter adapter =new MyAdapter(this,getData());
        mlistView.setAdapter(adapter);

    }
    private List<News>getData(){
        List<News>list =new ArrayList<>();
        News new1 =new News(R.drawable.qwe,"企鹅","么么哒");
                list.add(new1);
        News new2 =new News(R.drawable.asd,"女生","么么哒");
        list.add(new2);
        News new3 =new News(R.drawable.zxc,"老司机","么么哒");
        list.add(new3);

        News new4 =new News(R.drawable.qwe,"企鹅","么么哒");
        list.add(new4);
        News new5 =new News(R.drawable.asd,"女生","么么哒");
        list.add(new5);
        News new6 =new News(R.drawable.zxc,"老司机","么么哒");
        list.add(new6);
        News new7 =new News(R.drawable.qwe,"企鹅","么么哒");
        list.add(new7);
        News new8 =new News(R.drawable.asd,"女生","么么哒");
        list.add(new8);
        News new9 =new News(R.drawable.zxc,"老司机","么么哒");
        list.add(new9);
        return  list;
    }
}
 


package com.example.user.android2lesson_day3_baseadapter;

import android.content.Context;
import android.text.Layout;
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.List;

import bean.News;

/**
 * Created by user on 2016/7/6.
 */
public class MyAdapter extends BaseAdapter {
//    4.1声明属性,保存数据和上下文对象
    private List<News>list;
    private Context context;

    public  MyAdapter(Context context,List<News>list){
        this.list=list;
        this.context=context;
    }
    @Override
    public int getCount() {
        return list.size();
    }

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

    @Override
//    获取Item对应的ID
    public long getItemId(int i) {
        return i;
    }

    @Override
//    最最最关键的!!!!  获取i对应位置的视图
    public View getView(int i, View view, ViewGroup viewGroup
    ) {
//        优化前的方式!!!!!!!!
//        去获取指定的layout文件中指定id的控件
//        View v=LayoutInflater.from(context).inflate(R.layout.item_layout,null);
//        ImageView imageView = (ImageView) v.findViewById(R.id.image);
//        TextView titleView= (TextView) v.findViewById(R.id.text1);
//        TextView infoView = (TextView) v.findViewById(R.id.text2);
        拿到我们的数据
//        News news = (News) getItem(i);
        根据数据对界面进行赋值
//imageView.setBackgroundResource(news.getImg());
//        titleView.setText(news.getTitle().toString());
//        infoView.setText(news.getInfo().toString());
//        return v;
//        优化后的方式!!!!!!!
//        利用缓存
        ViewHolder  holder;
        if (view ==null){
//            将指定的布局文件转换成视图对象

//            这里是缓存的界面控件
            view =View.inflate(context,R.layout.item_layout,null);
             holder =new  ViewHolder();
            holder.imageView=(ImageView) view.findViewById(R.id.image);
//            ImageView image = (ImageView) view.findViewById(R.id.image);
            holder . titleView= (TextView) view.findViewById(R.id.text1);
            holder. InfoView= (TextView) view.findViewById(R.id.text2);
            view.setTag(holder);


        }else {holder = (ViewHolder) view.getTag();

        }  News news = (News) getItem(i);
        holder. imageView.setBackgroundResource(news.getImg());
        holder. titleView.setText(news.getTitle().toString());
        holder.InfoView.setText(news.getInfo().toString());
        return  view;
    }
}
***************************************************************************************
package bean;

/**
 * Created by user on 2016/7/6.
 */
public class News {
//    存放图片,资源都是采用id的形式存储
    private  int img;
    private  String title;
    private  String info;

//    初始化赋值方法
    public News(int img,String title,String info){
        this.img =img;
        this.title=title;
        this.info=info;
    }

    public int getImg() {
        return img;
    }

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

    public String getTitle() {
        return title;
    }

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

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }
}




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值