如何从主要活动获取数据到意图

时间:2016-07-26 15:27:23

标签: android android-studio android-intent rss

我正在制作一个RSS阅读器,并希望从显示为卡片视图的主要活动中获取数据并将其放入滚动活动中,我已经完成了这项活动。单击卡片视图时会打开此活动。我想用来自rss的一些数据填充滚动活动。 这是我的主要活动的代码

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);
    FeedItem current=feedItems.get(position);
    holder.Title.setText(current.getTitle());
    holder.Description.setText(current.getDescription());
    holder.Date.setText(current.getPubDate());
    Picasso.with(context).load(current.getThumbnailUrl()).into(holder.Thumbnail);
    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent=new Intent(context,NewsDetails.class);
            context.startActivity(intent);
        }
    });

}

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

public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView Title,Description,Date;
    ImageView Thumbnail;
    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_View);
        Thumbnail= (ImageView) itemView.findViewById(R.id.thumb_img);
        cardView= (CardView) itemView.findViewById(R.id.cardview);

    }
}

}

这是我的RSS阅读器的代码

public class ReadRss extends AsyncTask<Void,Void,Void> {
Context context;
String address="http://thirdtryrun.weebly.com/uploads/8/4/8/1/84817908/second.xml";
ProgressDialog progressDialog;
ArrayList<FeedItem>feedItems;
RecyclerView recyclerView;
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() {
    super.onPreExecute();
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    MyAdapter adapter=new MyAdapter(context,feedItems);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.addItemDecoration(new VerticalSpace(50));
    recyclerView.setAdapter(adapter);
}

@Override
protected Void doInBackground(Void... Void) {
    ProcessXml(Getdata());
    return null;
}

private void ProcessXml(Document data) {
    if (data!=null) {
        feedItems=new ArrayList<>();
        Element root=data.getDocumentElement();
        Node channel=root.getChildNodes().item(1);
        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("Description")){
                        item.setDescription(current.getTextContent());
                    }else if (current.getNodeName().equalsIgnoreCase("pubDate")){
                            item.setPubDate(current.getTextContent());
                    }else if (current.getNodeName().equalsIgnoreCase("link")){
                        item.setLink(current.getTextContent());
                    }else if (current.getNodeName().equalsIgnoreCase("content")){
                        item.setContent(current.getTextContent());
                    }else if (current.getNodeName().equalsIgnoreCase("media:thumbnail")){
                        String url=current.getAttributes().item(0).getTextContent();
                        item.setThumbnailUrl(url);
                    }
                }
                feedItems.add(item);
            }
        }
    }
}

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

}

这是我的Feed项的代码

public class FeedItem {
String title;
String link;
String description;
String pubDate;
String thumbnailUrl;
String content;

public String getTitle() {
    return title;
}

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

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

public String getThumbnailUrl() {
    return thumbnailUrl;
}

public void setThumbnailUrl(String thumbnailUrl) {
    this.thumbnailUrl = thumbnailUrl;
}

public String getPubDate() {
    return pubDate;
}

public void setPubDate(String pubDate) {
    this.pubDate = pubDate;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getLink() {
    return link;
}

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

}

2 个答案:

答案 0 :(得分:0)

In your current Activity, create a new Intent:

Intent i = new Intent(getApplicationContext(), NewActivity.class);

i.putExtra("key","value");
startActivity(i);
Then in the new Activity, retrieve those values:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("key");
    //The key argument here must match that used in the other activity
}
Use this technique to pass variables from one Activity to the other.

答案 1 :(得分:0)

@Saqib Khalil走在正确的轨道上,但如果getApplicationContext()给你带来问题,请尝试:YourActivity.this。如果您使用片段,则可以采用相同的方式传递信息,但将YourActivity.this替换为getActivity()

希望这有帮助!