如何获取HashMap的第一个元素

ven*_*ore 0 java android listview hashmap android-listview

尝试了很多方法并阅读了大量的文字,但我仍然无法理解如何解决我的问题.我们的想法是,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);
Run Code Online (Sandbox Code Playgroud)

适配器构造函数:

 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;
            } 
    };
Run Code Online (Sandbox Code Playgroud)

lvSimple.setAdapter(sAdapter);

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

map.keySet()
Run Code Online (Sandbox Code Playgroud)

那我有以下几点:

[date,image,text]
Run Code Online (Sandbox Code Playgroud)

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

Juv*_*nis 5

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

Map<String, Object> map = new LinkedHashMap<String, Object>();
Run Code Online (Sandbox Code Playgroud)

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

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