2014-02-06 79 views
2

我已存儲的一些值到ArrayList HashMap中像這樣:在ArrayList中的HashMap <字符串,字符串>檢索從鍵/值值

ArrayList<HashMap<String, String>> bookDetails = new ArrayList<HashMap<String, String>>(); 

HashMap<String, String> map = new HashMap<String, String>(); 

       map.put("book_author", bookAuthor); 
       map.put("book_description", bookDescription); 


       bookDetails.add(map); 

我簡單地希望能夠中檢索的描述值,並且具有其在TextView中顯示,我該如何去做?提前致謝。

回答

3

像這樣的東西應該工作:

TextView text = (TextView) findViewById(R.id.textview_id); 
text.setText(bookDetails.get(0).get("book_description")); 

改爲調用get(0)你當然也可以遍歷數組bookDetails並獲得當前迭代計數器變量,例如中get(n)

3

是否真的有必要?

爲什麼不創建一個Book.java對象

Book對象具有2屬性

public class Book { 

    private String bookAuthor; 
    private String bookDescription; 

    public String getBookAuthor() { 
     return bookAuthor; 
    } 
    public void setBookAuthor(String bookAuthor) { 
     this.bookAuthor = bookAuthor; 
    } 
    public String getBookDescription() { 
     return bookDescription; 
    } 
    public void setBookDescription(String bookDescription) { 
     this.bookDescription = bookDescription; 
    } 

} 

然後你就可以擁有的書籍一個列表。

1

我建議改變你存儲信息的方式。

如果你的地圖只包含作者和描述,一個有效的方法是完全省略ArrayList並且只使用Map。

地圖將

HashMap<String, String> map; 
map.put(bookAuthor, bookDescription); 

訪問的描述會更容易,以及:

String desc = map.get(bookAuthor); 

希望這有助於。

相關問題