我只有一个问题。我做了一个清单,如下:
List<Map<String, String>> ShopsList = new ArrayList<Map<String,String>>();
private void initList() {
// We add the cities
ShopsList.add(createShop("Antwerpen", "Broer Bretel"));
ShopsList.add(createShop("Antwerpen", "Caffènation"));
ShopsList.add(createShop("Antwerpen", "Caffènation - Take Out Nation"));
ShopsList.add(createShop("Antwerpen", "Coffeelabs"));
ShopsList.add(createShop("Antwerpen", "De Dikke Kat"));
ShopsList.add(createShop("Antwerpen", "Mlle Loustache"));
ShopsList.add(createShop("Berchem", "Broer Bretel"));
ShopsList.add(createShop("Berchem", "Caffènation"));
ShopsList.add(createShop("Berchem", "Caffènation - Take Out Nation"));和
private HashMap<String, String> createShop(String key, String name) {
HashMap<String, String> shop = new HashMap<String, String>();
shop.put(key, name);
return shop;
}所以现在我使用SimpleAdapter在Listview中显示这个列表。但我想要的是能够只显示来自列表的数据与特定的关键字。我就是这么做的
ListView lv = (ListView) findViewById(R.id.listView);
SimpleAdapter simpleAdpt = new SimpleAdapter(this, ShopsList, android.R.layout.simple_list_item_1,
new String[] {"Antwerpen"}, new int[] {android.R.id.text1});
lv.setAdapter(simpleAdpt);当我这样做时,他只向我显示具有正确关键字的数据,但将另一个条目的添加为空。因此,当我请求第二个关键字时,他首先添加了6个空位,然后才显示正确的条目。
我该怎么做呢?我想我应该用way关键字添加条目的位置,但是我如何以一种简单的方式检索这些位置?
谢谢!
发布于 2013-04-05 22:02:10
编辑:
正如所指出的,我把SimpleAdapter和ArrayAdapter弄错了。很抱歉,如果你想把你的实现改成ArrayAdapter (这要简单得多),代码如下。
原创:
在用值填充android.R.id.text1时,ArrayAdapter仅在列表中的每个元素中调用.toString()。
有几种方法可以实现你想要的。
其中之一就是制作一个List<String>,然后按照你想在屏幕上看到的那样制作Strings。
例如:
public class Shop{
private String city;
private String name;
public Shop(String city, String name){
this.city = city;
this.name = name;
}
@Override
public String toString() {
return city + " - " + name
}
}然后在您的适配器上,您将使用List<Shop>而不是map。通过覆盖toString()方法,您可以随心所欲地操作文本。
我只想提一下,另一种方法可以是扩展ListAdapter类
https://stackoverflow.com/questions/15835574
复制相似问题