SimpleAdapter结合ListView的使用

一.创建activity_session.xml

-1.代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#99117700"
 android:orientation="vertical" >

    <ListView
 android:id="@+id/lv_session"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fadingEdgeLength="0dp"
 android:fastScrollEnabled="true"
 android:paddingBottom="10dp"
 android:transcriptMode="normal" >
    </ListView>

</LinearLayout>
  • 1

二.创建item_session

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

<?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="wrap_content"
 android:id="@+id/item_list"
 android:orientation="vertical" >

    <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:minHeight="65dp"
 android:orientation="horizontal"
 android:paddingTop="5.0dip" >

        <ImageView
 android:id="@+id/iv_recent_avatar"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_gravity="center_vertical"
 android:layout_marginLeft="10dp"
 android:layout_weight="0.2"
 android:contentDescription="@string/hello_world"
 android:src="@drawable/ali_044" />

        <LinearLayout
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_weight="0.8"
 android:orientation="vertical" >

            <TextView
 android:id="@+id/tv_recent_name"
 android:layout_width="wrap_content"
 android:layout_height="25.0dip"
 android:layout_marginLeft="10.0dip"
 android:layout_marginRight="58.0dip"
 android:ellipsize="end"
 android:gravity="left|center"
 android:singleLine="true"
 android:text="xiaomin" />

            <TextView
 android:id="@+id/tv_recent_msg"
 android:layout_width="fill_parent"
 android:layout_height="35.0dip"
 android:layout_marginLeft="5dp"
 android:layout_marginRight="35.0dip"
 android:ellipsize="end"
 android:gravity="center_vertical"
 android:paddingLeft="6.0dip"
 android:paddingRight="6.0dip"
 android:singleLine="true"
 android:text="目前画面中真心难看 体谅啊。。。"
 android:textSize="12.0sp" />
        </LinearLayout>

        <LinearLayout
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_weight="0.4"
 android:orientation="vertical" >

            <TextView
 android:id="@+id/tv_recent_time"
 android:layout_width="wrap_content"
 android:layout_height="25.0dip"
 android:layout_marginRight="10dip"
 android:gravity="center"
 android:padding="0.0dip"
 android:singleLine="true"
 android:text="今天 16:09"
 android:layout_gravity="right"
 android:textColor="#99000000"
 android:textSize="10.0sp" />

            <TextView
 android:id="@+id/tv_recent_unread"
 android:layout_width="16dip"
 android:layout_height="16.0dip"
 android:layout_marginRight="6.0dip"
 android:layout_marginTop="5.0dip"
 android:background="@drawable/list_newmessage2"
 android:gravity="center"
 android:layout_gravity="right"
 android:textColor="#ffffffff"
 android:textSize="10.0dip" />
        </LinearLayout>
    </LinearLayout>

    <View
 android:layout_width="match_parent"
 android:layout_height="2dp"
 android:layout_gravity="bottom"
 android:background="#99000000" >
    </View>


</LinearLayout>
  • 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<UserInfo> userInfos = new ArrayList<UserInfo>();
        List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

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

        for (UserInfo info : userInfos) {
            HashMap<String, Object> item = new HashMap<String, Object>();

            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);

    }

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

G11176593

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值