2014-07-17 46 views
0

嘗試了很多方法並閱讀大量文本,但我仍然無法理解如何解決我的問題。這個想法是有一個HashMap,用來自數據庫的數據填充循環「for」。此外,這個參數(HashMap)被傳遞給listViewContent,然後創建適配器並最終填充listView。事實是,listView中的每一行都有2個textView和1個imageView,我需要從這個listView的第二個textView中獲取文本,數據存儲在數據庫中。如何獲得HashMap的第一個元素

這是我的代碼塊: 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] 

,我想知道我怎麼可以得到該地圖的「文本」鍵的第一個元素。 我只需要第一個文本,因爲我想將此文本放在通知正文中。

回答

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); 
+0

是的,這是我真正想要的!但現在我有另一個問題:我的地圖在方法中,但我需要獲取第一個元素,並將其放入onCreate方法中的String。我怎樣才能做到這一點?我試圖創建一個變量並將其分配給地圖,但後來我得到NullPointer – vendettacore

+1

@vendettacore我無法遠程調試您的代碼。檢索第一個條目並將其存儲在全局變量中,然後在任何需要的地方使用它。你的代碼太混亂了,對不起,這是我的建議。 – Juvanis

1

HashMap沒有「first」的概念。如果您想保留迭代順序,請改爲使用LinkedHashMap

+0

好吧,如果我將使用LinkedHashMap,我怎樣才能得到「文本」鍵的第一個元素? – vendettacore

+0

它將是密鑰集迭代器返回的第一個元素。你有什麼嘗試? –

+0

我的「文本」鍵充滿了數據庫中的字符串,我只需要獲取第一個字符串。例如:「第一個字符串」,「第二個字符串」,「第三個字符串」....我只需要得到第一個。 – vendettacore

0

HashMap s爲無序,沒有拉出的第一個條目的概念。

您可能需要使用一個LinkedHashMap

0

我不會低估它錯誤,你要拿到鑰匙的名字從你的地圖「文本」是如此

嘗試

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

我的「文本」鍵從數據庫中逐個填充字符串,我只需要獲取第一個字符串。例如:「第一個字符串」,「第二個字符串」,「第三個字符串」....我只需要得到第一個。 – vendettacore