草庐IT

android - 从 hashmap 获取值并添加到 listview

coder 2023-12-29 原文

我有一个哈希表

HashMap<String, Integer> map = new HashMap<String, Integer>();

map 中有一些值。我想一个一个地获取值并将其添加到 ListView 中。 map 中的值是

{Intent { cmp=Bluetooth/300 }=300, Intent { cmp=Audio/400 }=400, Intent { cmp=Video/500 }=500, Intent { cmp=Display/100 }=100, Intent { cmp=WiFi/200 }=200}

listview中有两个textview。 我想在 ListView 中显示为

Display 100

WiFi 200

Bluetooth 300.

现在我公开我的 Adapter 类,这将对您有所帮助...

private class NewAdapter extends BaseAdapter {

        public NewAdapter(IntentTestingActivity intentTestingActivity,
                HashMap<String, Integer> map) {
        }

        @Override
        public int getCount() {
            Log.d(TAG, "Map size is: " + map.size());
            return map.size();
        }

        @Override
        public Object getItem(int arg0) {
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            return 0;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {

            View v = view;

            if (v == null) {

                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                v = vi.inflate(R.layout.class_name, null);

            }

            TextView className = (TextView) v.findViewById(R.id.name);
            TextView tagName = (TextView) v.findViewById(R.id.tag_name);

            Integer key_name;
            key_name = map.get(name);

            Log.d(TAG, "Complete map is: " + map.toString());

            // String tag = map.get(tagName).toString();
            // Integer name = map.get(className);

            String keyName;
            keyName = map.toString();
            Log.d(TAG, "KeyName is: " + map.get(tag));

            for (int i = 0; i < map.size(); ++i)
                Log.d(TAG, "Tag is: " + tag + " and Name is: " + name + " and Intent is: "+intent);

            HashMap<String, Integer> hashmap = map;
            for (Entry<String, Integer> e : hashmap.entrySet()) {
                String key = e.getKey();
                int value = e.getValue();

                Set<String>keyname = map.keySet();

            Log.d(TAG, "Key: " + key+ " Value: "+value);
            }


            className.setText(name.toString());
//           tagName.setText(keyName);
            return v;
        }

    }

其中 name 是一个字符串,其中包含所有 keyValue,例如 Display、Vedio 等。

提前致谢...

最佳答案

您可以使用 getter-setter 创建一个 POJO 类,并将键和值设置到该类。

        List<POJO> list = new ArrayList<POJO>();
        Iterator<Entry<String, Integer>> iterator = map.entrySet().iterator();
        Entry< String, Integer> entry;
        while(iterator.hasNext()){
            POJO obj = new POJO();
            entry = iterator.next();
            Log.d("Key Value",entry.getKey()+" "+entry.getValue());
            obj.setKey(entry.getKey());
            obj.setValue(entry.getValue());
            list.add(obj);
        }

然后将此 list 设置为您的 Adapter 类。这将是一个简单的方法。

关于android - 从 hashmap 获取值并添加到 listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9125678/

有关android - 从 hashmap 获取值并添加到 listview的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  3. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  4. ruby - 简单获取法拉第超时 - 2

    有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url

  5. ruby - 从 Ruby 中的主机名获取 IP 地址 - 2

    我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge

  6. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

  7. ruby-on-rails - 获取 inf-ruby 以使用 ruby​​ 版本管理器 (rvm) - 2

    我安装了ruby​​版本管理器,并将RVM安装的ruby​​实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby​​。有没有办法让emacs像shell一样尊重ruby​​的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el

  8. Ruby 从大范围中获取第 n 个项目 - 2

    假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit

  9. ruby - Net::HTTP 获取源代码和状态 - 2

    我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur

  10. ruby - 可以通过多少种方法将方法添加到 ruby​​ 对象? - 2

    当谈到运行时自省(introspection)和动态代码生成时,我认为ruby​​没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby​​的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资

随机推荐