我试图从安装在 android 设备示例的包中显示可绘制对象:“com.android.browser”以显示应用程序的图标。我已经用这个布局设置了一个 sherlocklistfragment
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"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/appName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/numOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
然后我使用 hashmap 来处理 simpleAdapter 的数据
夏洛克列表片段
public class List_fragment extends SherlockListFragment {
private PackageManager pk;
String[] apps = new String[] {
"Browser",
"Email",
"Facebook",
"Clock",
"Map",
"Calendar",
"Settings",
"Media player",
"Google play",
"testapp"
};
String[] period = new String[]{
"54",
"23",
"42",
"23",
"14",
"23",
"23",
"1",
"10",
"12"
};
public final static String TABLE_LOGS = "logs";
public final static String ID_COL = "_id";
public final static String DATE_COL = "date";
public final static String PACKAGE_COL = "package";
public final static String LAT_COL = "lat";
public final static String LNG_COL = "lng";
public final static String DUR_COL = "duration";
View view;
private OnItemClickListener mOnItemClickListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
List<HashMap<String,Object>> aList = new ArrayList<HashMap<String,Object>>();
pk = getSherlockActivity().getPackageManager();
Drawable drawable = null;
for(int i=0;i<10;i++){
HashMap<String, Object> hm = new HashMap<String,Object>();
hm.put("app", "" + apps[i]);
hm.put("per", period[i] + " seconds");
try {
drawable = pk.getApplicationIcon("com.android.browser");
//hm.put("image", drawable);
hm.put("image", R.drawable.down_arrow);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
aList.add(hm);
}
Log.d("DRAWABLE", " "+ drawable);
SimpleAdapter listAdapter = new SimpleAdapter(inflater.getContext(), aList,
R.layout.list_layout, new String[] {"image", "app", "per"}, new int[] {R.id.imageView, R.id.appName, R.id.numOpen});
//listAdapter.setViewBinder(mViewBinder);
setListAdapter(listAdapter);
Log.d("String " , " " + aList);
return super.onCreateView(inflater, container, savedInstanceState);
}
private final SimpleAdapter.ViewBinder mViewBinder = new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
// TODO Auto-generated method stub
if(view instanceof ImageView){
((ImageView) view).setImageDrawable((Drawable) data);
}
return false;
}
};
}
编辑:代码
这显示了 appName 的文本和 imageview 上的箭头图标,但它确实显示了我在简单适配器中插入的可绘制对象?请参阅此注释行:
//hm.put("image", drawable);
我究竟做错了什么?