从 ArrayList HashMap<String, String> 中的键/值中检索值



我已经将一些值存储在acraylist 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 text = (TextView) findViewById(R.id.textview_id);
text.setText(bookDetails.get(0).get("book_description"));

而不是调用get(0),您当然也可以在bookDetails数组上迭代并获取当前迭代计数器变量,例如get(n)

真的必要吗?

为什么不创建 book.java对象

书籍对象具有 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;
    }
}

然后您可以拥有书籍列表

我建议更改您存储信息的方式。

如果您的地图仅包含作者和描述,则有效的方法是完全省略Arraylist并仅使用地图。

地图将是

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

访问描述也将更容易:

String desc = map.get(bookAuthor);

希望这会有所帮助。

相关内容

最新更新