在recyclerView中显示元素列表失败

时间:2019-06-29 03:38:18

标签: java android android-recyclerview

我正在尝试制作一个新闻应用程序,该数据是从RSS feed获得的数据。我从提要中得到xml响应。我正在使用XmlPullParser解析xml。解析是成功的。我可以在日志中看到这些值。

但是以某种方式,我无法在recyclerView中填充它们。都是空白。我的Java类是:

public class RssNewsActivity extends AppCompatActivity {
private static final String TAG = RssNewsActivity.class.toString();

RssFeedAdapter adapter;
RecyclerView recyclerView_rssFeed;
SwipeRefreshLayout swipeRefreshLayout;

private List<RssModel> listResponseModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rss_news);

    initialize();

    recyclerView_rssFeed.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    recyclerView_rssFeed.setLayoutManager(linearLayoutManager);

    adapter = new RssFeedAdapter(RssNewsActivity.this, listResponseModel);
    recyclerView_rssFeed.setAdapter(adapter);

    fetchNewsFeed();

    //if user swipes the recycler then refresh content page
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            fetchNewsFeed();
        }
    });
}

private void initialize() {
    swipeRefreshLayout = findViewById(R.id.swipeRefresh_rssFeed);
    recyclerView_rssFeed = findViewById(R.id.recyclerView_rssFeed);

    listResponseModel = new ArrayList<>();
}

private void fetchNewsFeed() {
    String url = "here is my news feed url";
    StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            listResponseModel.clear();
            try {
                listResponseModel = parseNewsFeed(response);
            } catch (XmlPullParserException | IOException e) {
                Log.e(TAG, e.getMessage());
            }
            adapter.notifyDataSetChanged();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    RequestQueue queue = Volley.newRequestQueue(this);
    queue.add(request);

}

private List<RssModel> parseNewsFeed(String response) throws XmlPullParserException,
        IOException {

    XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
    parserFactory.setNamespaceAware(true);
    XmlPullParser xmlPullParser = parserFactory.newPullParser();
    xmlPullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
    xmlPullParser.setInput(new StringReader(response));

    return processParsing(xmlPullParser);
}

private List<RssModel> processParsing(XmlPullParser xmlPullParser) throws IOException, XmlPullParserException {
    List<RssModel> listRssFeed = new ArrayList<>();
    int eventType = xmlPullParser.getEventType();
    RssModel rssModel = null;

    xmlPullParser.nextTag();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        String eltName;
        switch (eventType) {
            case XmlPullParser.START_DOCUMENT:
                break;
            case XmlPullParser.START_TAG:
                eltName = xmlPullParser.getName();
                if ("item".equals(eltName)) {
                    rssModel = new RssModel();
                } else if (rssModel != null) {
                    if ("title".equals(eltName)) {
                        String title = xmlPullParser.nextText();
                        rssModel.setTitle(title);
                        Log.d(TAG, "title: " + title);
                    } else if ("link".equals(eltName)) {
                        String link = xmlPullParser.nextText();
                        rssModel.setLink(link);
                        Log.d(TAG, "link: " + link);
                    }
                }
                break;
            case XmlPullParser.END_TAG:
                eltName = xmlPullParser.getName();
                if ("item".equals(eltName) && rssModel != null) {
                    listRssFeed.add(rssModel);
                }
                break;
        }
        eventType = xmlPullParser.next();
    }
    return listRssFeed;
}
}

我的xml设计是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="rssnewsfeed.RssNewsActivity">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefresh_rssFeed"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView_rssFeed"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>

这是我的适配器类

public class RssFeedAdapter extends RecyclerView.Adapter<RssFeedAdapter.RssViewHolder> {
private Context mContext;
private List<RssModel> mRssFeeds;

public RssFeedAdapter(Context mContext, List<RssModel> mRssFeeds) {
    this.mContext = mContext;
    this.mRssFeeds = mRssFeeds;
}

@NonNull
@Override
public RssViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.rss_feed_layout, viewGroup, false);
    return new RssViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull RssViewHolder rssViewHolder, int i) {
    RssModel post = mRssFeeds.get(i);

    rssViewHolder.textView_title.setText(post.getTitle());
    rssViewHolder.textView_link.setText(post.getLink());
}

@Override
public int getItemCount() {
    return mRssFeeds.size();
}

public class RssViewHolder extends RecyclerView.ViewHolder {
    private TextView textView_title, textView_link;

    public RssViewHolder(@NonNull View itemView) {
        super(itemView);
        textView_title = itemView.findViewById(R.id.textView_title);
        textView_link = itemView.findViewById(R.id.textView_link);
    }

}
}

最后这是我的模型课:

public class RssModel {
private String title;
private String link;

public RssModel() {
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getLink() {
    return link;
}

public void setLink(String link) {
    this.link = link;
}
}

我尝试了很多方法,但没有成功。我不知道我在想什么。如有任何指导,我将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:1)

提供“ =”将创建ArrayList的新实例,除此之外,该实例已绑定到RecyclerView适配器。

所以,您需要更改它,

listResponseModel = parseNewsFeed(response);

listResponseModel.addAll(parseNewsFeed(response));

答案 1 :(得分:1)

之所以发生这种情况,是因为您传递到回收站视图适配器的Rss列表为空,如前所述,您已成功获取日志中的值,因此另一个有保证的解决方法是将这些值设置到单独的arraylist中。

因此在您的类中,只需创建arraylist:

private ArrayList<String> title;

private ArrayList<String> post;

这将是您的初始化方法:

private void initialize() 
{
       swipeRefreshLayout = findViewById(R.id.swipeRefresh_rssFeed);
       recyclerView_rssFeed = findViewById(R.id.recyclerView_rssFeed);

       title = new ArrayList<>();

       post = new ArrayList<>();

}

现在只需将您的值传递给arraylist:

title.add(title)

post.add(post)

现在将那些arraylist传递给适配器:

adapter = new RssFeedAdapter(RssNewsActivity.this, title,post);
    recyclerView_rssFeed.setAdapter(adapter);

在适配器中初始化arraylist之后,获取您的值!

oncreate()内还有一件重要的事情,在初始化适配器之前放置fetchNewsFeed();方法

 initialize();
fetchNewsFeed();
  recyclerView_rssFeed.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    recyclerView_rssFeed.setLayoutManager(linearLayoutManager);

    adapter = new RssFeedAdapter(RssNewsActivity.this, listResponseModel);
    recyclerView_rssFeed.setAdapter(adapter);