2016-07-26 77 views
-1

我正在製作一個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; 
    } 
} 

}

這裏的代碼是爲我的飼料項目

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

}

+1

可能的重複[如何在Android上的活動之間傳遞數據?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android) – ChrisStillwell

回答

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. 
+0

當我這樣做getApplicationContext是紅色的 –

0

@Saqib哈利勒是在正確的軌道上,但如果getApplicationContext()是給你的問題,請嘗試:YourActivity.this來代替。如果使用碎片,則可以用相同的方式傳遞信息,但用getActivity()替換YourActivity.this

希望這會有所幫助!

相關問題