Android 在Sherlocklistfragment中显示可绘制的自定义布局
我正在尝试显示安卓设备上安装的软件包中的可绘图项,例如:“com.android.browser”,以显示应用程序的图标。 我已经用这个布局设置了一个sherlocklistfragment XMLAndroid 在Sherlocklistfragment中显示可绘制的自定义布局,android,hashmap,actionbarsherlock,drawable,Android,Hashmap,Actionbarsherlock,Drawable,我正在尝试显示安卓设备上安装的软件包中的可绘图项,例如:“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_p
<?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>
我做错了什么?来自
对于参数至(最后一个参数是imageView1和textView1:
应在“from”参数中显示列的视图。这些视图都应为文本视图。此列表中的前N个视图具有
from参数中前N列的值
有很多方法可以做到这一点,但首先,您的片段布局应该有一个
ListView
屏幕布局
ListFragment具有由单个列表视图组成的默认布局。但是,如果需要,可以通过从onCreateView(LayoutFlater、ViewGroup、Bundle)返回自己的视图层次结构来自定义片段布局。为此,视图层次结构必须包含id为“@android:id/list”的ListView对象(或列表,如果是代码)
或者,您的视图层次结构可以包含任何类型的另一个视图对象,以便在列表视图为空时显示。此“空列表”通知程序必须具有id“android:empty”。请注意,当存在空视图时,列表视图将在无数据显示时隐藏
当您使用SherlockListFragment
时,您的布局必须具有预定义的android:id=@android:id/list
行布局
您可以指定列表中各行的布局。您可以通过在片段承载的ListAdapter对象中指定布局资源来完成此操作(ListAdapter将ListView绑定到数据;稍后将对此进行详细介绍)
ListAdapter构造函数接受一个参数,该参数为每行指定一个布局资源。它还有两个附加参数,用于指定要与行布局资源中的哪个对象关联的数据字段。这两个参数通常是并行数组
必须使用单独的xml定义行布局,或为其动态构建行布局
每行。第一个选项更常见
正如@Johannes对SimpleAdapter
所说的那样,您必须只有TextView
。您可以只使用TextView
使用自定义布局的示例。因此,您必须使用/扩展另一个listapter
,例如ArrayAdapter
甚至BaseAdapter
最重要的是方法getView
,它负责
返回将放在列表视图
每一行上的视图。在这种方法中,必须将行布局充气并填充
此链接显示a。您可以查看此链接,以了解您到底需要做什么。我现在已经解决了我的问题。谢谢您的帮助 通过使用,我创建了一个row.xml布局文件和一个自定义游标适配器,如下所示: 光标适配器
public class MyCursorAdapter extends SimpleCursorAdapter{
public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to){
super(context, layout, c, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = getSherlockActivity().getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
TextView text1 = (TextView)row.findViewById(R.id.text1);
TextView text2 = (TextView)row.findViewById(R.id.text2);
ImageView image1 = (ImageView)row.findViewById(R.id.image1);
Cursor cursor = adapter.getCursor();
cursor.moveToPosition(position);
try {
info = pk.getApplicationInfo(cursor.getString(cursor.getColumnIndex(PACKAGE_COL)), 0);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String title = (String) pk.getApplicationLabel(info);
text1.setText(title);
String duration = cursor.getString(cursor.getColumnIndex(DUR_COL));
text2.setText("App open for " + duration + " seconds");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Drawable d = info.loadIcon(pk);
image1.setImageDrawable(d);
return row;
}
}
布局
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;
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/image1"
android:layout_width="75dp"
android:layout_height="75dp"
android:src="@drawable/ic_launcher"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
android:textSize="22dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text2"
android:textSize="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/image2"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/arrow"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
谢谢你的回答。我不知道这将如何帮助我将drawable android.graphics.drawable.bitmapdrawables分配给简单适配器或给定布局文件的imageview。音乐艺术家列表视图的链接没有解决我的问题。这仍然“只是”从绘图表获取资源。请更正我的错误。此外,为什么我需要listview来显示自定义列表视图?列表正在显示,但仅显示TextView。在使用带有图像的适配器时,是否确实需要使用listview?抱歉,但我有点困惑。您问题的主要点是,您必须实现自定义列表布局,因为Android只为基本列表提供适配器。要在列表中放置特定的可绘制内容,您应该实现ListAdapter的方法getView。这与您的可绘制内容无关,无论它是否可绘制资源。这不重要。如果您的问题是正确获取可绘制内容,我认为您应该更改您的问题。Cor如果我错了,请纠正我,你没有列表,你有一个线性布局,对吗?好的,我的解释可能有点不清楚。我确实得到一个列表,显示文本和图像,但仅图像作为资源ex:R.drawable.arrow。请参阅新插入的代码。我要做的是使用以下命令获取设备上安装的应用程序的图标。drawable=pk.getApplicationCon(“com.android.browser”);如果从上面尝试使用drawable,则会收到以下消息:resolveUri在错误位图uri上失败:android.graphics.drawable。BitmapDrawable@40694728If我需要做我自己的自定义适配器,然后你们可以帮助如何最好地做到这一点?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/image1"
android:layout_width="75dp"
android:layout_height="75dp"
android:src="@drawable/ic_launcher"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
android:textSize="22dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text2"
android:textSize="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/image2"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/arrow"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>