适配器,顾名思义,是为了连接某些不能直接关联的数据或控件等,android为我们提供了多种多样的适配器,但往往不能达到自己理想的效果,这里就简单总结了怎样自定义适配器的方法。
这里,我们的程序是获取当前已安装的应用名称以及图标,并将其显示在ListView中:
由于代码简单,所以就木有写注释。。。
首先,我们先完成布局文件item.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"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/imageView_icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="Large Text"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
然后是主 MainActivity
package com.wx.test_getallapps;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
private Context context;
private List<PackageInfo> packageInfos;
private List<Map<String, Object>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_main);
packageInfos = getPackageManager().getInstalledPackages(0);
data = new ArrayList<Map<String, Object>>();
for (int i = 0; i < packageInfos.size(); i++) {
PackageInfo packageInfo = packageInfos.get(i);
Map<String, Object> map = new HashMap<String, Object>();
map.put("icon",
packageInfo.applicationInfo.loadIcon(getPackageManager()));
map.put("name",
packageInfo.applicationInfo.loadLabel(getPackageManager())
.toString());
data.add(map);
}
MyAdapter myAdapter = new MyAdapter(context, data);
ListView listView = (ListView) findViewById(R.id.listView_item_checked);
listView.setAdapter(myAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
最后登场的就是 MyAdapter了
package com.wx.test_getallapps;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private List<Map<String,Object>> data = null;
private LayoutInflater mlInflater;
public MyAdapter(){
}
public MyAdapter(Context context, List<Map<String, Object>> data) {
// TODO Auto-generated constructor stub
this.data = data;
mlInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if(convertView == null){
convertView = mlInflater.inflate(R.layout.item, null);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView)convertView.findViewById(R.id.imageView_icon);
viewHolder.textView = (TextView)convertView.findViewById(R.id.textView_name);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
Map<String , Object> map = data.get(position);
viewHolder.imageView.setImageDrawable((Drawable) map.get("icon"));
viewHolder.textView.setText((CharSequence) map.get("name"));
return convertView;
}
class ViewHolder{
ImageView imageView;
TextView textView;
}
}