繁体   English   中英

如何将数组列表值从我的活动传递到自定义适配器?

[英]How to pass arraylist values from my activity to custom adapter?

我正在创建一个示例新闻应用程序。 我将 rss 提要存储在哈希列表中并将其放在数组列表中。 我有一个自定义的 rcycler 视图适配器。 我想将此值传递给适配器并以网格形式显示。 但是现在我无法传递和显示值。

  @Override
    protected String doInBackground(String... args) {
        // rss link url
        String rss_url = args[0];

        // list of rss items
        rssItems = rssParser.getRSSFeedItems(rss_url);

        // looping through each item
        for (RSSItem item : rssItems) {
            // creating new HashMap
            if (item.link.toString().equals(""))
                break;
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value

            String givenDateString = item.pubdate.trim();
            SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy 
    HH:mm:ss Z");
            try {
                Date mDate = sdf.parse(givenDateString);
                SimpleDateFormat sdf2 = new SimpleDateFormat("EEEE, dd MMMM 
   yyyy - hh:mm a", Locale.US);
                item.pubdate = sdf2.format(mDate);

            } catch (ParseException e) {
                e.printStackTrace();
            }

            map.put(TAG_TITLE, item.title);
            map.put(TAG_LINK, item.link);
            map.put(TAG_DESCRIPTION, item.description);
            map.put(TAG_PUB_DATE, item.pubdate); // If you want parse the 
     date

            // adding HashList to ArrayList
            rssItemList.add(map);
        }

        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {

                RecyclerView recyclerView = (RecyclerView) 
  findViewById(R.id.recyclerView);
                // set a GridLayoutManager with default vertical orientation 
    and 2 number of columns
                GridLayoutManager gridLayoutManager = new 
    GridLayoutManager(getApplicationContext(), 2);
                recyclerView.setLayoutManager(gridLayoutManager); // set 
     LayoutManager to RecyclerView
                //  call the constructor of CustomAdapter to send the 
     reference and data to Adapter
                CustomAdapter customAdapter = new 
      CustomAdapter(RSSFeedActivity.this, rssItemList, new String[] 
   {TAG_LINK, TAG_TITLE, TAG_DESCRIPTION,TAG_PUB_DATE});
                recyclerView.setAdapter(customAdapter);
            }
        });
        return null;
    }

Recyclleview 适配器:

   import android.content.Context;
   import android.content.Intent;
   import android.support.annotation.NonNull;
   import android.support.v7.widget.RecyclerView;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.ImageView;
   import android.widget.TextView;
    import java.util.ArrayList;
         public class CustomAdapter extends  
       RecyclerView.Adapter<CustomAdapter.MyViewHolder>  {
    ArrayList rssItemList;
      String [] from;

Context context;
public CustomAdapter(Context context, ArrayList rssItemList,String[] from) {
    this.context = context;
    this.rssItemList = rssItemList;
    this.from=from;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // infalte the item Layout
    View v = 
   LayoutInflater.from(parent.getContext()).inflate(R.layout.gridview_row, 
     parent, false);
    // set the view's size, margins, paddings and layout parameters
    MyViewHolder vh = new MyViewHolder(v); // pass the view to View Holder
    return vh;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {



    holder.name.setText(rssItemList.indexOf(0)); //Not working
}


@Override
public int getItemCount() {
    return rssItemList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
    // init the item view's
    TextView name;
    TextView image;
    public MyViewHolder(View itemView) {
        super(itemView);
        // get the reference of item view's
        name = (TextView) 
        itemView.findViewById(R.id.android_gridview_text1);
        image = (TextView) 
        itemView.findViewById(R.id.android_gridview_text2);
    }
        }

项目类

public class RSSItem {

public String title;
public String link;
public String description;
public String pubdate;
public String guid;

public RSSItem(String title, String link, String description, String 
 pubdate, String guid) {
    this.title = title;
    this.link = link;
    this.description = description;
    this.pubdate = pubdate;
    this.guid = guid;
     }
  }

我该如何解决这个问题? 请检查我的客户适配器代码。

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

确保 rssItemList 有数据然后 onBindViewHolder 应该像这样

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.name.setText(rssItemList.get(position).getTitle().toString());
//rssItemList.get(position).getYourMethod().toString()
 }

你的实现是正确的,但这不是正确的holder.name.setText(rssItemList.indexOf(0));

而是使用这样的东西;

HashMap item = rssItemList.get(position)
holder.name.setText(item.get("title"))

indexOf() 方法是Returns the index of the first occurrence of the specified element索引,它用于查找索引,而您只想拥有列表的 RssItem。 这就是为什么我在onBindViewHolder使用 get 和变量position

编辑:你的数组充满了 HashMap,我已经更新了第一行的代码,你得到了你的 HashMap,在第二行你得到了具有键“title”的变量,如果你输入“link”,你可以获得另一个值", "description" 或 pubDate" 代替

尝试下面的代码从数组列表中获取值

  • holder.name.setText(rssItemList.get(postion).get(TAG_TITLE))

暂无
暂无

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

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