【问题标题】:How to get the first element of HashMap如何获取HashMap的第一个元素
【发布时间】:2014-09-08 10:48:14
【问题描述】:

尝试了很多方法并阅读了大量文本,但我仍然无法理解如何解决我的问题。这个想法是有一个 HashMap 填充了一个循环“for”,其中包含来自数据库的数据。此外,这个参数(HashMap)被传递给listViewContent,然后创建适配器并最终填充listView。事实上,listView 中的每一行都有 2 个 textView 和 1 个 imageView,我需要从这个 listView 的第二个 textView 中获取文本,数据存储在数据库中。

这是我的代码块: HashMap 块

if (posts != null) {
    for (WallPostItem post : posts) {
        //create new map for a post
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(ATTRIBUTE_NAME_TEXT, post.text);
        PictureItem postPicture = new PictureItem();
        map.put(ATTRIBUTE_NAME_IMAGE, postPicture);
        map.put(ATTRIBUTE_NAME_DATE, post.date);



        if ((post.pictures != null) && (post.pictures.size() != 0)) {
            //start downloading of pictures

            String pictureLink = post.pictures.get(0);

            if (dbHandler.hasPicture(pictureLink)) {
                Bitmap image = dbHandler.getPicture(pictureLink);
                int width = image.getWidth();
                int height = image.getHeight();
                if (width>800 || height>800){
                 int threeWidth = width / 2;
                 int threeHeight = height / 2;
                 Bitmap bmThree = Bitmap.createScaledBitmap(image, threeWidth,
                        threeHeight, false);

                postPicture.picture = bmThree;}
                else if (width>500 && width <800 || height>500 && height<800) {
                    int halfWidth = width;
                    int halfHeight = height;
                    Bitmap bmHalf = Bitmap.createScaledBitmap(image, halfWidth, halfHeight, false);
                    postPicture.picture = bmHalf;
                } else postPicture.picture = image;
            //Log.d("mytest", " HAS PICTURE " + pictureLink);
            } else {
                DownloadImageTask downloadImageTask = new DownloadImageTask(postPicture){
                    @Override
                    public void onResponseReceived(){
                    //Log.d("mytest", "adding picture to db: " + urldisplay);
                        if (bmImage.picture != null)
                            dbHandler.addPicture(bmImage.picture, urldisplay);

                        sAdapter.notifyDataSetChanged();
                    };
                };
                downloadImageTask.execute(pictureLink);
            }
        }
        listViewContent.add(map);

适配器构造函数:

 String[] from = {ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_IMAGE, ATTRIBUTE_NAME_DATE};
    int[] to = {R.id.tvText, R.id.ivImg, R.id.tvDate};

    sAdapter = new SimpleAdapter(this, listViewContent, R.layout.news_item, from, to) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {  
            View view = super.getView(position, convertView, parent);  
                if ((position % 2) == 0) {
                    view.setBackgroundColor(Color.parseColor("#00E9E9E9"));
                } else {
                    view.setBackgroundColor(Color.parseColor("#00F5F5F5"));
                }
                return view;
            } 
    };

lvSimple.setAdapter(sAdapter);

我也尝试获取我的地图的键,我这样做了 -

map.keySet()

那么我有以下内容:

[date,image,text]

我想知道如何获取此地图的“文本”键的第一个元素。 我只需要第一个文本,因为我想将此文本放在通知正文中。

【问题讨论】:

    标签: java android listview android-listview hashmap


    【解决方案1】:

    如果您使用LinkedHashMap,将保留元素的插入顺序。

    Map<String, Object> map = new LinkedHashMap<String, Object>();
    

    然后,要从地图中检索第一个插入的条目,您可以使用:

    List<Entry<String, Object>> list = 
              new ArrayList<Entry<String, Object>>(map.entrySet());
    Entry<String, Object> firstInsertedEntry = list.get(0);
    

    【讨论】:

    • 是的,这正是我想要的!但是现在我有另一个问题:我的地图在方法中,但我需要获取第一个元素并将其放入 onCreate 方法中的 String 中。我怎样才能做到这一点?我试图创建一个变量并将其分配给映射,但后来我得到了 NullPointer
    • @vendettacore 我无法远程调试您的代码。检索第一个条目并将其存储在全局变量中,然后在需要的地方使用它。你的代码太乱了,对不起,这是我可以建议的。
    【解决方案2】:

    HashMap 没有“第一”的概念。如果您想保留迭代顺序,请改用LinkedHashMap

    【讨论】:

    • 好的,如果我要使用 LinkedHashMap 如何获取“text”键的第一个元素?
    • 它将是键集迭代器返回的第一个元素。你试过什么?
    • 我的“文本”键一个一个地填充数据库中的字符串,我只需要获取第一个字符串。例如:“第一个字符串”、“第二个字符串”、“第三个字符串”……我只需要得到第一个。
    • 它将是键集迭代器返回的第一个元素。你试过什么?
    • @vendettacore 请检查我的答案!
    【解决方案3】:

    HashMaps是无序的,没有拉出第一个条目的概念。

    您可能想使用LinkedHashMap

    【讨论】:

      【解决方案4】:

      我并没有低估你需要从地图中获取键名作为“文本”的错误

      试试

      String text =  map.get("text");
      

      【讨论】:

      • 我的“文本”键一个一个地填充数据库中的字符串,我只需要获取第一个字符串。例如:“第一个字符串”、“第二个字符串”、“第三个字符串”……我只需要得到第一个。
      【解决方案5】:

      hashmap 不保留插入顺序。所以最好使用 LinkedHashMap 来实现您要查找的内容。如果您想了解更多信息,请查看本文http://java.dzone.com/articles/hashmap-vs-treemap-vs

      【讨论】:

        猜你喜欢
        • 2017-12-24
        • 2012-06-22
        • 1970-01-01
        • 2012-05-08
        • 2011-05-04
        • 1970-01-01
        • 1970-01-01
        • 2022-08-17
        • 1970-01-01
        相关资源
        最近更新 更多