Reputation: 59
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 :)
Upvotes: 0
Views: 1762
Reputation: 24848
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;
}
}
Upvotes: 1
Reputation: 1379
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());
Upvotes: 1