创建自定义hashmapadapter [英] Creating a custom hashmapadapter

查看:144
本文介绍了创建自定义hashmapadapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个包含以下一个HashMap:

 的Hashmap包含(字符串,字符串)

我如何实例化一个自定义适配器?自定义适配器应该扩展baseadapter。
我需要将这两个键和值相结合,所以它看起来像KEY +价值,KEY + VALUE......并指定这一个数组值。数组值以后使用时,我in​​santiate我自定义adpter上。

实例化应该是这个样子:

  MyCustomAdapter适配器=新MyCustomAdapter(这一点,android.R.layout.simple_list_item_1,价值观);
setListAdapter(适配器)

我在这里失去了code将是一个很大的帮助。

感谢
格雷厄姆

下面的列表使用其数据源,一个名为项字符串数组。

 公开的ArrayList<串GT; myList中=新的ArrayList<串GT;(Arrays.asList(项目));

但项目是一个字符串数组,我想停止使用,转而开始使用级联键+值对从我的HashMap

所以不是用户是$ P $的psented的项目清单,他将是presented从散HM拍摄键+值对的列表


解决方案

我不认为你需要使用自定义适配器。您的布局很简单,你只需要一个TextView,所以你可以使用ArrayAdapter。
因为你比如你可以这样做:

 的HashMap<整数,字符串> HM =新的HashMap<整数,字符串>();
矢量<串GT;元素=新的矢量<串GT;();


 的for(int i = 0; I< = 10;我){
      hm.put(I,(民+ i)段);
    }
    对于(进入<整数,字符串> E:hm.entrySet()){
        串newString = e.toString();
        elements.add(newString);
    }
    ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,元素);
    list.setAdapter(适配器);


If I have a hashmap containing the following:

Hashmap contains (String, String)

How can I instantiate a custom adapter? The custom adapter should extend baseadapter. I need to combine both key and value so it looks something like "KEY+VALUE", "KEY+VALUE"... and assign this to an array VALUES. The array VALUES is used later on when I insantiate my custom adpter.

Instantiation should look something like this:

MyCustomAdapter adapter = new MyCustomAdapter(this, android.R.layout.simple_list_item_1, VALUES);   
setListAdapter(adapter)

I am lost here so code would be a big help.

THANKS graham

The following list is using as its datasource a string array called items.

public ArrayList<String> myList = new ArrayList<String>(Arrays.asList(items)); 

however items is a string array which I would like to stop using and instead start using concatenated key+value pairs from my hashmap

so instead of the user being presented a list of items he will be presented a list of key+value pairs taken from the hashmap hm

解决方案

I don't think you need to use a custom adapter. Your layout is quite simple, you need only a textView, so you can use ArrayAdapter. For you example you can do:

HashMap<Integer,String>hm=new HashMap<Integer,String>();
Vector<String>elements=new Vector<String>();

 for(int i=0; i<=10;i){      
      hm.put(i,("num"+i));
    }
    for (Entry<Integer, String> e : hm.entrySet()) {
        String newString=e.toString();
        elements.add(newString);
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, elements);
    list.setAdapter(adapter);

这篇关于创建自定义hashmapadapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆