[英]Android Adapters - Which One To Use?
I am still unclear about which adapter to use in a situation that doesn't require the basic, SimpleAdapter. 我仍然不清楚在不需要基本的SimpleAdapter的情况下使用哪个适配器。 There are BaseAdapters, ArrayAdapters, CustomAdapters, etc. 有BaseAdapters,ArrayAdapters,CustomAdapters等。
I would like to make a ListView with a simple layout like seen in the comment section in the Google PlayStore. 我想制作一个具有简单布局的ListView,就像在Google PlayStore的注释部分中看到的那样。 A TextView on one side, and an image that pops up a context menu of some sort. 一侧为TextView,以及弹出某种上下文菜单的图像。
What adapter would I use for this that would work best? 我将为此使用哪种适配器最有效?
In your case it is easier to use a SimpleAdapter
. 在您的情况下,使用SimpleAdapter
更容易。 Just provide your custom layout and connect the data with the widgets' id. 只需提供您的自定义布局并将数据与小部件的ID连接即可。 Something similar to this: 类似于以下内容:
List<HashMap<String,String>> datalist = new ArrayList<HashMap<String,String>>();
HashMap<String, String> map = new HashMap<String,String>();
map.put("text", "some text");
map.put("image",Integer.toString( R.drawable.your_image_to_popup_a_menu ));
datalist.add( map );
String[] from = { "text","image" };
int[] to = { R.id.txt,R.id.img };
SimpleAdapter adapter = new SimpleAdapter( this, datalist, R.layout.your_layout, from, to);
Do not forget to identify the layout's TextView and ImageView as "@+id/txt"
and "@+id/img"
respectively. 不要忘记将布局的TextView和ImageView分别标识为"@+id/txt"
和"@+id/img"
。
(If your image is always the same, just set it in the layout and forget linking the R.id.img
part) (如果您的图像始终相同,则只需在布局中进行设置,而R.id.img
链接R.id.img
部分)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.