简体   繁体   English

如何从Hashmap获取ArrayList <String String>

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

Here I have fetched data from server/json and populated my ArrayList 在这里,我已从服务器/ 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);

Somewhere else (within my Adapter Class), I want to populate a ListView using this data, and i get the data like: 在其他地方(在我的适配器类中),我想使用此数据填充ListView,并且得到如下数据:

 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. 然后,在构建单个List项时,我有两个TextView,必须在其中加载新闻标题和新闻日期。

 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 :) 任何想法,...对于JAVA和Android都是新的:)

I would prefer creating a separate model class called News as follows 我希望创建一个单独的名为News模型类,如下所示

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. 然后,可以使用ArrayList<News>进行保存,而不是使用HashMap

To render the fields in getView , 要在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());

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

暂无
暂无

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

相关问题 如何获取 HashMap <String,ArrayList<String> &gt; 从给定的字符串 Java - How to get an HashMap<String,ArrayList<String>> from a given String Java 从HashMap获得价值 <String, ArrayList<String> &gt; - Get value from HashMap<String, ArrayList<String>> 如何从ArrayList添加值 <HashMap<String, String> &gt; menuItems = new ArrayList <HashMap<String, String> &gt;编辑文本 - How to add values from ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>> to Edit Text 从ArrayList获取密钥 <HashMap<String, String> &gt;&gt;放入另一个数组 - Get Keys from ArrayList<HashMap<String, String>>> into another array 如何从ArrayList中获取所有元素 <HashMap<String, String> &gt;安卓 - How to get all the elements out of ArrayList<HashMap<String, String>> android 如何使用Arraylist中的bundle使用putserializable获取传递的值 <Hashmap<String,String> &gt; - How to get valus passed using putserializable using bundle from Arraylist<Hashmap<String,String>> 如何保存ArrayList <HashMap<String, String> 从返回函数? - How to save the ArrayList<HashMap<String, String>> from return function? 如何从ArrayList中删除null {}映射 <HashMap<String, String> &gt;? - How to remove null { } map from ArrayList<HashMap<String, String>>? 如何存储一个ArrayList <HashMap<String, String> &gt;具有单个Hashmap和多个Arraylist - How to store an a ArrayList<HashMap<String, String>> with a single Hashmap and multiple Arraylist 如何从Arraylist中删除所选项目 <HashMap<String, String> &gt;在android中 - How to remove the selected item from Arraylist<HashMap<String, String>> in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM