0

Here I have fetched data from server/json and populated my ArrayList

ArrayList<HashMap<String, String>> newsUpdateList = new ArrayList<HashMap<String, String>>();
private static final String TAG_NEWSCAPTION = "caption";
private static final String TAG_NEWSDATE = "date";

 HashMap<String, String> newsUpdate = new HashMap<String, String>();
            //adding each child node to HashMap key => value
            newsUpdate.put(TAG_NEWSCAPTION, newsCaption);
            newsUpdate.put(TAG_NEWSDATE, newsDate);
            newsUpdateList.add(newsUpdate);

Somewhere else (within my Adapter Class), I want to populate a ListView using this data, and i get the data like:

 HashMap<String, String> newsItem = newsUpdateList.get(position);

And then in building the individual List items, I have two TextViews in which i have to load the news caption, and news date.

 TextView caption = (TextView) v.findViewById(R.id.newsCaption);
 TextView date = (TextView) v.findViewById(R.id.newsDate);

Here's the problem, what is the proper way the extract the caption and date from the

 HashMap<String, String> newsItem

Any ideas, ...new to JAVA and Android :)

3
  • HashMap associate a Key and a Value. There is two objects here. And List has only one dimension. So how do you want these two objects become one? Commented Aug 23, 2014 at 9:24
  • Check this : stackoverflow.com/questions/18683130/… Commented Aug 23, 2014 at 9:30
  • Thanks Simon & Haresh. @Haresh, your answer was exactly what I needed. Could you put this as an answer so I can accept it? Commented Aug 23, 2014 at 9:51

2 Answers 2

1

Try this way,hope this will help you to solve your problem.

public class PendingAdapter extends BaseAdapter {

    private List<Map<String, Object>> mPendingItemList;

    public PendingAdapter() {
        mPendingItemList = DataModel.getInstance().getPendingItemList();
    }

    @Override
    public int getCount() {
        return mPendingItemList.size();
    }

    @Override
    public Object getItem(int position) {
        return mPendingItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {

            convertView = LayoutInflater.from(getActivity()).inflate(
                    R.layout.pending_item, null);
            holder = new ViewHolder();

            holder.tv_title = (TextView) convertView
                    .findViewById(R.id.pi_tv_title);
            holder.tv_content = (TextView) convertView
                    .findViewById(R.id.pi_tv_content);
            holder.tv_counter = (TextView) convertView
                    .findViewById(R.id.pi_tv_counter);
            holder.tv_ongoing = (TextView) convertView
                    .findViewById(R.id.pi_tv_ongoing);
            holder.tv_type = (TextView) convertView
                    .findViewById(R.id.pi_tv_type);
            holder.tv_date = (TextView) convertView
                    .findViewById(R.id.pi_tv_date);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        @SuppressWarnings("unchecked")
        HashMap<String, String> itemDataHashMap = (HashMap<String, String>) getItem(position);

        holder.tv_title.setText(itemDataHashMap.get("planet"));
        holder.tv_content.setText(itemDataHashMap.get("content"));
        holder.tv_counter.setText(itemDataHashMap.get("counter"));
        holder. tv_type.setText(itemDataHashMap.get("type"));
        holder.tv_ongoing.setText(itemDataHashMap.get("ongoing"));
        holder.tv_date.setText(itemDataHashMap.get("date"));

        convertView.setTag(holder);
        return convertView;
    }

    static class ViewHolder {
        TextView tv_title;
        TextView tv_content;
        TextView tv_counter;
        TextView tv_ongoing;
        TextView tv_type;
        TextView tv_date;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I would prefer creating a separate model class called News as follows

public class News {
    private String caption;
    private String date;

    // getters and setters for both caption and date
}

Then, instead of using a HashMap, you can directly use ArrayList<News> to save.

To render the fields in getView,

News news = newsUpdateList.get(position);
TextView caption = (TextView) v.findViewById(R.id.newsCaption);
TextView date = (TextView) v.findViewById(R.id.newsDate);

caption.setText(news.getCaption());
date.setText(news.getDate());

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.