SimpleAdapter结合ListView的使用

一.创建activity_session.xml

-1.代码如下



    
    

  • 1

二.创建item_session

给每一个item设置自定义的布局




    

        

        

            

            
        

        

            

            
        
    

    
    


  • 1

三.创建UserInfo.java

创建用户对象 存储用户的一些信息 这里只是模拟 关于如何访问数据库,后面更新

public class UserInfo {

    private int iv_recent_avatar;
    private String tv_recent_name;
    private String tv_recent_msg;
    private String tv_recent_time;
    private String tv_recent_unread;


    public UserInfo(int iv_recent_avatar, String tv_recent_name,
            String tv_recent_msg, String tv_recent_time, String tv_recent_unread) {
        super();
        this.iv_recent_avatar = iv_recent_avatar;
        this.tv_recent_name = tv_recent_name;
        this.tv_recent_msg = tv_recent_msg;
        this.tv_recent_time = tv_recent_time;
        this.tv_recent_unread = tv_recent_unread;
    }

    public int getIv_recent_avatar() {
        return iv_recent_avatar;
    }

    public void setIv_recent_avatar(int iv_recent_avatar) {
        this.iv_recent_avatar = iv_recent_avatar;
    }

    public String getTv_recent_name() {
        return tv_recent_name;
    }

    public void setTv_recent_name(String tv_recent_name) {
        this.tv_recent_name = tv_recent_name;
    }

    public String getTv_recent_msg() {
        return tv_recent_msg;
    }

    public void setTv_recent_msg(String tv_recent_msg) {
        this.tv_recent_msg = tv_recent_msg;
    }

    public String getTv_recent_time() {
        return tv_recent_time;
    }

    public void setTv_recent_time(String tv_recent_time) {
        this.tv_recent_time = tv_recent_time;
    }

    public String getTv_recent_unread() {
        return tv_recent_unread;
    }

    public void setTv_recent_unread(String tv_recent_unread) {
        this.tv_recent_unread = tv_recent_unread;
    }

}
  • 1

四.创建SessionActivity.java

实现数据的显示 使用SimpleAdapter

package com.example.im002.session;

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

import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.im002.R;
import com.example.im002.config.BaseActivity;

public class SessionActivity extends BaseActivity {

    private ListView lv_session;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_session);

        initView();

        show();

    }

    private void show() {

        final UserInfo info1 = new UserInfo(R.drawable.ali_001png, "哎_愁啊",
                "这些数据呢 是我自己给出的 不好", "今天 19:20", "77");
        final UserInfo info2 = new UserInfo(R.drawable.ali_002, "甭愁了_赶紧些",
                "优化 优化 快点优化", "今天 15:20", "9");
        final UserInfo info3 = new UserInfo(R.drawable.ali_044, "CPU快点转",
                "界面太丑了 静下心来 ", "昨天 19:20", "");

        List userInfos = new ArrayList();
        List> data = new ArrayList>();

        userInfos.add(info1);
        userInfos.add(info2);
        userInfos.add(info3);

        for (UserInfo info : userInfos) {
            HashMap item = new HashMap();

            item.put("iv_recent_avatar", info.getIv_recent_avatar());
            item.put("tv_recent_name", info.getTv_recent_name());
            item.put("tv_recent_msg", info.getTv_recent_msg());
            item.put("tv_recent_time", info.getTv_recent_time());
            item.put("tv_recent_unread", info.getTv_recent_unread());

            data.add(item);

        }

        SimpleAdapter adapter = new SimpleAdapter(SessionActivity.this, data,
                R.layout.item_session, new String[] { "iv_recent_avatar",
                        "tv_recent_name", "tv_recent_msg", "tv_recent_time",
                        "tv_recent_unread" }, new int[] {
                        R.id.iv_recent_avatar, R.id.tv_recent_name,
                        R.id.tv_recent_msg, R.id.tv_recent_time,
                        R.id.tv_recent_unread });

        lv_session.setAdapter(adapter);

    }

    public void initView() {

        lv_session = (ListView) findViewById(R.id.lv_session);

    }

}

未完待续。。。。。。后面再完善这个说明 做详细的解释 欢迎各位大牛 指点

你可能感兴趣的:(大数据)