gpt4 book ai didi

java - 在 recyclerView 中显示元素列表失败

转载 作者:行者123 更新时间:2023-12-02 01:26:35 25 4
gpt4 key购买 nike

我正在尝试制作一个新闻应用程序,从 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;
}
}

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

最佳答案

给出“=”将创建一个ArrayList的新实例,除此之外已经绑定(bind)到RecyclerView适配器。

所以,你需要改变这个,

listResponseModel = parseNewsFeed(response);

listResponseModel.addAll(parseNewsFeed(response));

关于java - 在 recyclerView 中显示元素列表失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56814903/

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