如何获取HashMap的第一个元素

时间:2014-07-17 14:00:20

标签: java android listview android-listview hashmap

尝试了很多方法并阅读了大量的文字,但我仍然无法理解如何解决我的问题。我们的想法是,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]

我想知道如何获得&#34; text&#34;的第一个元素。这张地图的关键。 我只需要第一个文本,因为我想将此文本放在通知正文中。

5 个答案:

答案 0 :(得分:5)

如果您使用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);

答案 1 :(得分:1)

HashMap没有“first”的概念。如果要保留迭代顺序,请改用LinkedHashMap

答案 2 :(得分:0)

HashMap 无序,没有提取第一个条目的概念。

您可能想要使用LinkedHashMap

答案 3 :(得分:0)

我没有说错,你需要从你的地图中获取关键名称“text”

尝试

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

答案 4 :(得分:0)

hashmap不保留插入顺序。所以最好使用LinkedHashMap来实现你想要的东西。如果你想了解更多http://java.dzone.com/articles/hashmap-vs-treemap-vs

,请查看这篇文章。