繁体   English   中英

如何从Hashmap获取ArrayList <String String>

[英]How to get ArrayList from Hashmap<String String>

在这里,我已从服务器/ 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都是新的:)

我希望创建一个单独的名为News模型类,如下所示

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

    // getters and setters for both caption and date
}

然后,可以使用ArrayList<News>进行保存,而不是使用HashMap

要在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());

尝试这种方式,希望这将帮助您解决问题。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM