一、SimpleAdapter 简要说明
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
官方说明了其各个参数含义,我这里根据自己的理解解释下:
第一个context,很明显大家根据英文可以知道是上下文的意思,它官方的意思是:SimpleAdapter所要运行关联到的视图,这个是什么呢?就是你这个SimpleAdapter所在的Activity(一般而言),所以这个参数一般是this
第二个是一个泛型只要是一个List就行,这一般会想到是ArrayList,而他内部存储的则是Map或者继承自Map的对象,比如HashMap,这些语法都是Java的基本语法,不再详述了!这里呢是作为数据源,而且每一个ArraList中的一行就代表着呈现出来的一行,Map的键就是这一行的列名,值也是有列名的。
第三个资源文件,就是说要加载这个两列所需要的视图资源文件,你可以左边一个TextView右边一个TextView,目的在于呈现左右两列的值!
第四个参数是一个数组,主要是将Map对象中的名称映射到列名,一一对应
第五个是将第四个参数的值一一对象的显示(一一对应)在接下来的int形的id数组中,这个id数组就是LayOut的xml文件中命名id形成的唯一的int型标识符
一、SimpleAdapter实例,Android 显示安装程序列表
XML文件 infos.xml
<?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="match_parent">
<ListView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/itemlist"></ListView>
</LinearLayout>
XML文件 info_row.xml
<?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="match_parent">
<TextView android:text="TextView" android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="TextView" android:id="@+id/desc" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>java文件
package cf.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class software extends Activity implements Runnable {
List<Map<String, Object>> list = null;
ListView itemlist = null;
private ProgressDialog pd;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.infos);
setTitle("软件信息");
itemlist = (ListView) findViewById(R.id.itemlist);
pd = ProgressDialog.show(this, "请稍候..", "正在收集你已经安装的软件信息...", true,
false);
Thread thread = new Thread(this);
thread.start();
}
private void refreshListItems() {
list = fetch_installed_apps();
SimpleAdapter notes = new SimpleAdapter(this, list, R.layout.info_row,
new String[] { "name", "desc" }, new int[] { R.id.name,
R.id.desc });
itemlist.setAdapter(notes);
setTitle("软件信息,已经安装"+list.size()+"款应用.");
}
public List fetch_installed_apps(){
List<ApplicationInfo> packages = getPackageManager().getInstalledApplications(0);
list = new ArrayList<Map<String, Object>>(
packages.size());
Iterator<ApplicationInfo> l = packages.iterator();
while (l.hasNext()) {
Map<String, Object> map = new HashMap<String, Object>();
ApplicationInfo app = (ApplicationInfo) l.next();
String packageName = app.packageName;
String label = "";
try {
label = getPackageManager().getApplicationLabel(app).toString();
} catch (Exception e) {
Log.i("Exception",e.toString());
}
map = new HashMap<String, Object>();
map.put("name", label);
map.put("desc", packageName);
list.add(map);
}
return list;
}
@Override
public void run() {
fetch_installed_apps();
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
refreshListItems();
pd.dismiss();
}
};
}
本文详细解析了Android中使用SimpleAdapter显示安装程序列表的过程,包括SimpleAdapter的各项参数含义及其作用,并通过XML文件和Java代码实例进行了具体实现。
1579

被折叠的 条评论
为什么被折叠?



