gpt4 book ai didi

java - 如何从 Hashmap 获取 ArrayList

转载 作者:行者123 更新时间:2023-12-02 05:31:50 27 4
gpt4 key购买 nike

这里我从 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);

然后在构建各个列表项时,我有两个 TextView,我必须在其中加载新闻标题和新闻日期。

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

问题是,从

中提取标题和日期的正确方法是什么
 HashMap<String, String> newsItem

任何想法,...JAVA 和 Android 新手:)

最佳答案

试试这个方法,希望能帮助您解决问题。

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

关于java - 如何从 Hashmap<String String> 获取 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25460409/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com