2014-02-06 80 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); 

希望这有助于。

相关问题