android中ListView_SimpleAdapter

时间:2024-01-06 21:09:20

1.首先看下main_activity.xml。其实里面就放了一个ListView。

<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:orientation="vertical"
tools:context=".MainActivity" > <ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="" >
</ListView> </LinearLayout>

2.接着我们看下适配器中要放的布局(我们把他称之为小布局文件)。

<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:orientation="horizontal"
tools:context=".MainActivity" > <ImageView
android:id="@+id/imvpicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> <TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#16CCDD"
android:textSize="22sp"
android:text=""/> <TextView android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#666666"
android:textSize="16sp"
android:text=""/> </LinearLayout> </LinearLayout>

3.接着我们看下ListView代码。(activity)

package com.example.listview_simpleadapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView; public class MainActivity extends Activity {
ListView lv = null;
ImageView imgpicture = null;
TextView tvtitle =null;
TextView tvinfo = null;
String title [] = {"图片","音乐","视频"};
String info [] = {"美辰良景,给你无限的遐思,让人感觉无限温馨……","轻曼音乐,令人如入仙境,如痴如醉……","震撼场景,360度的视觉捕获,一览无遗……"};
Integer imv[] = {R.drawable.tupian,R.drawable.yinyue,R.drawable.shiping};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] from = {"imv","title","info"};
int[] to = {R.id.imvpicture,R.id.title,R.id.info};
lv = (ListView)super.findViewById(R.id.listView1);
imgpicture = (ImageView)super.findViewById(R.id.imvpicture);
tvtitle = (TextView)super.findViewById(R.id.title);
tvinfo = (TextView)super.findViewById(R.id.info);
lv.setAdapter(new SimpleAdapter(this, getData(), R.layout.simple_small, from, to)); }
private List<? extends Map<String, ?>> getData() {
List list = new ArrayList();
for (int i = ; i<imv.length; i++){
Map map = new HashMap();
map.put("imv", imv[i]);
map.put("title", title[i]);
map.put("info", info[i]);
list.add(map);
}
return list;
}
}

4.运行结果

android中ListView_SimpleAdapter

其实里面还可以放更多的东西,只要你想放。本文适合有基础的爱好者学习,仅供参考。由于自己现在在做项目,许多地方来不及备注,有什么不懂的地方可以留言,空了给你解答。