我正在创建一个包含RSS源的Android应用程序。目前,我已经将应用程序编程为从一个xml提要中获取数据并显示我想要的方式。但是,我不确定如何在程序中添加多个xml提要。我希望该应用能够显示来自多个Feed /链接的数据。我目前正在直接从网址上提取我的单一Feed。
我是Java编程和Android Studio的初学者,所以这可能就是我无法解决这个问题的原因!有人建议使用txt文件来保存所有的feed链接,但我不确定如何编写代码。
如果您需要的不仅仅是以下文件,请告诉我们。我想我已经包含了相关文件。
道歉,任何帮助都会受到高度赞赏!
ReadRSS.java:
package com.example.liutaurasmazonas.cslogintrying;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.provider.DocumentsContract;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import static android.R.attr.data;
/**
* Created by Joe on 04/03/2017.
*/
public class ReadRss extends AsyncTask<Void,Void,Void>{
ArrayList<FeedItem>feedItems;
RecyclerView recyclerView;
Context context;
String address="https://www.bloomberg.com/feeds/podcasts/etf_report.xml";
ProgressDialog progressDialog;
URL url;
public ReadRss(Context context, RecyclerView recyclerView){
this.recyclerView=recyclerView;
this.context=context;
progressDialog=new ProgressDialog(context);
progressDialog.setMessage("Loading...");
}
@Override
protected void onPreExecute() {
progressDialog.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
MyAdapter adapter=new MyAdapter(context,feedItems);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.addItemDecoration(new VerticalSpace(50));
recyclerView.setAdapter(adapter);
}
@Override
protected Void doInBackground(Void... params) {
ProcessXml(Getdata());
return null;
}
private void ProcessXml(Document data) {
if (data != null){
feedItems=new ArrayList<>();
Element root=data.getDocumentElement();
Node channel=root.getChildNodes().item(0);
NodeList items=channel.getChildNodes();
for (int i=0;i<items.getLength();i++){
Node currentchild=items.item(i);
if (currentchild.getNodeName().equalsIgnoreCase("item")){
FeedItem item=new FeedItem();
NodeList itemchilds=currentchild.getChildNodes();
for (int j=0;j<itemchilds.getLength();j++){
Node current=itemchilds.item(j);
if (current.getNodeName().equalsIgnoreCase("title")){
item.setTitle(current.getTextContent());
}else if (current.getNodeName().equalsIgnoreCase("itunes:summary")){
item.setDescription(current.getTextContent());
}else if (current.getNodeName().equalsIgnoreCase("pubDate")){
item.setPubDate(current.getTextContent());
}else if (current.getNodeName().equalsIgnoreCase("link")){
item.setLink(current.getTextContent());
}
}
feedItems.add(item);
// Log.d("itemTitle", item.getTitle());
// Log.d("itemDescription", item.getTitle());
// Log.d("itemLink", item.getTitle());
// Log.d("itemPubDate", item.getTitle());
}
}
}
}
public Document Getdata(){
try {
url=new URL(address);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream=connection.getInputStream();
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=builderFactory.newDocumentBuilder();
Document xmlDoc = builder.parse(inputStream);
return xmlDoc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
MyAdapter.java
package com.example.liutaurasmazonas.cslogintrying;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import org.w3c.dom.Text;
import java.util.ArrayList;
/**
* Created by Joe on 05/03/2017.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
ArrayList<FeedItem>feedItems;
Context context;
public MyAdapter(Context context,ArrayList<FeedItem>feedItems){
this.feedItems=feedItems;
this.context=context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.custum_row_news_item,parent,false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
YoYo.with(Techniques.FadeIn).playOn(holder.cardView);
final FeedItem current=feedItems.get(position);
holder.Title.setText(current.getTitle());
holder.Description.setText(current.getDescription());
holder.Date.setText(current.getPubDate());
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, NewsDetails.class);
intent.putExtra("Link", current.getLink());
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return feedItems.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView Title,Description,Date;
CardView cardView;
public MyViewHolder(View itemView) {
super(itemView);
Title= (TextView) itemView.findViewById(R.id.title_text);
Description=(TextView) itemView.findViewById(R.id.description_text);
Date=(TextView) itemView.findViewById(R.id.date_text);
cardView= (CardView) itemView.findViewById(R.id.cardview);
}
}
}
答案 0 :(得分:1)
您可以简单地将另一个RecyclerView添加到您希望显示多个Feed的XML Layout中,然后使用另一个Asynctask和Adapter从新的RecyclerView上的其他URL加载Feed。
像这样,
public class ReadRssFromUrl extends AsyncTask<Void,Void,Void>{
ArrayList<FeedItem>feedItems;
RecyclerView recyclerView1;
Context context;
String address="aDifferentUrl.xml";
ProgressDialog progressDialog;
URL url;
public ReadRss(Context context, RecyclerView recyclerView1){
this.recyclerView1=recyclerView1;
this.context=context;
progressDialog=new ProgressDialog(context);
progressDialog.setMessage("Loading...");
}
因此传递给此构造函数的RecyclerView应该是新构造函数。
希望这有帮助。