Android适配器之SimpleAdapter

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.miao.news2.MainActivity"
    android:orientation="vertical">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

news_layout.xml:

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

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="80dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/title"
            android:textSize="20sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/content"
            android:textSize="15sp"
            android:textColor="#0000ff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>
MainActivity.java:

package com.miao.news;

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

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

/*
**再做一次修改....
*/

public class MainActivity extends AppCompatActivity {
    private ListView lv ;
    private SimpleAdapter sAdapter;
    private List<Map<String,Object>> item;
    private Map<String,Object> map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView)findViewById(R.id.lv);

        sAdapter = new SimpleAdapter(this,getData(),R.layout.my_layout,
			new String[]{"title","info","image"},
			new int[]{R.id.title,R.id.info,R.id.image});

        lv.setAdapter(sAdapter);
    }

    public List<Map<String,Object>> getData(){
        item = new ArrayList<Map<String, Object>>();

        map = new HashMap<String,Object>();
        map.put("image",R.drawable.accident);
        map.put("title","Accident");
        map.put("info","今日早间新闻报道,四川一山区发生翻车事件。");
        item.add(map);

        map = new HashMap<String,Object>();
        map.put("image",R.drawable.phone);
        map.put("title","IPhone SE");
        map.put("info","IPhoneSE 将于本月的21号发布,敬请期待。");
        item.add(map);

        map = new HashMap<String,Object>();
        map.put("image",R.drawable.boy);
        map.put("title","Boy & Dog");
        map.put("info","男孩和两只狗狗的故事。");
        item.add(map);

        map = new HashMap<String,Object>();
        map.put("image",R.drawable.cat);
        map.put("title","Mom & Child");
        map.put("info","这只猫咪的孩子好奇怪,是不是生着生着没墨了。");
        item.add(map);

        return item;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值