java - 如何获取HashMap的第一个元素

标签 java android listview android-listview hashmap

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

这是我的代码块: HashMap block

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 的 key ,我做到了 -

map.keySet()

然后我得到以下内容:

[date,image,text]

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

最佳答案

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

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

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

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

关于java - 如何获取HashMap的第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24805664/

相关文章:

javascript - Phonegap - JavaScript 不工作

android - 您可以在 Android 中的相同两个设备之间建立多个蓝牙连接吗?

python - PyGTK 如何将 "load-on-demand"数据传到 TreeView

qt - QML:如何将模型属性传递给加载在 GridView(或 ListView)委托(delegate)中的委托(delegate)?

java - 改进 CoreNLP 词性标注器和 NER 标注器?

java - 如果重复出现,则在 while 循环中将列值相加

Java Spring MVC - 模型中类的动态属性名称

java - 如何将一个事件从一个按钮连接到另一个按钮?

android - 尝试从原始文件夹(VideoView)播放视频

android - 添加新项目时如何保留在我的 listView 项目中?