2

I have stored some values to an ArrayList HashMap like so:

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

I simply want to be able to retreive the description value and have it displayed in a TextView, how would I go about doing so? Thanks in advance.

0

3 Answers 3

3

Something like this should work:

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

Instead of call get(0) you can of course also iterate over the bookDetails Array and get the current iteration counter variable, e.g. get(n)

Sign up to request clarification or add additional context in comments.

Comments

3

Is a map really necessary?

Why not creating a Book.java object.

Book object has 2 attributes:

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;
    }

}

Then you can have a List of books.

Comments

1

I'd suggest to change the way you store information.

If your map contains only author and description, an effective way would be to omit ArrayList completely and use only Map.

The Map would be

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

Accessing the descriptions would be easier as well:

String desc = map.get(bookAuthor);

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.