这里我从server / json获取数据并填充了我的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);
在其他地方(在我的适配器类中),我想使用这些数据填充ListView,我得到的数据如下:
HashMap<String, String> newsItem = newsUpdateList.get(position);
然后在构建单个List项目时,我有两个TextView,我必须加载新闻标题和新闻日期。
TextView caption = (TextView) v.findViewById(R.id.newsCaption);
TextView date = (TextView) v.findViewById(R.id.newsDate);
问题在于,从
中提取标题和日期的正确方法是什么? HashMap<String, String> newsItem
任何想法,... JAVA和Android新手:)
答案 0 :(得分:1)
我更愿意创建一个名为News
的单独模型类,如下所示
public class News {
private String caption;
private String date;
// getters and setters for both caption and date
}
然后,您可以直接使用HashMap
进行保存,而不是使用ArrayList<News>
。
在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());
答案 1 :(得分:1)
请尝试这种方式,希望这有助于您解决问题。
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;
}
}