将 ArrayList 值传递给 Intent.putExtra for Intent.ACTION_SEND [英] Passing ArrayList value to a Intent.putExtra for Intent.ACTION_SEND
问题描述
如何将 arraylist 值(samplename, samplequote )传递给 intent.putExtra.. 以共享文本..
在 MainActivity.java
list = new ArrayList<>();
//loading list view item with this function
loadRecyclerViewItem();
}
private void loadRecyclerViewItem() {
list.add(new MyQuote("sample name","sample quote"));
在 MyRecyclerViewAdapter.java
public void onBindViewHolder(final MyRecycleViewAdapter.ViewHolder myholder, int position) {
final MyQuote myQuote = myQuoteList.get(position);
myholder.tv_author.setText(myQuote.getAuthor());
myholder.tv_quote.setText(myQuote.getQuotedesc());
myholder.im_favlike.setImageResource(R.drawable.fav_border);
myholder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent (Intent.ACTION_SEND);
intent.setType("text/plain");
view.getContext().startActivity(Intent.createChooser(intent,"send to"));
}
});
}
编辑在 MyQuote 类中实现 Parcelable 之后.. 就像这样,当我使用 intent.putParcelableArrayListExtra("mani" , MyQuote);....我得到预期的表达......在 MyRecyclerViewAdapter.java 中"
public class MyQuote implements Parcelable{
private String author;
private String quotedesc;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(quotedesc);
parcel.writeString(author);
}
private MyQuote(Parcel parcel){
quotedesc = parcel.readString();
author = parcel.readString();
}
public static final Parcelable.Creator<MyQuote> CREATOR = new Parcelable.Creator<MyQuote>(){
@Override
public MyQuote createFromParcel(Parcel parcel) {
return new MyQuote(parcel);
}
public MyQuote[] newArray(int size){
return new MyQuote[size];
}
};
//constructor initializing values
public MyQuote(String author, String quotedesc) {
this.quotedesc = quotedesc;
this.author = author;
}
//getters
public String getAuthor() {
return author;
}
public String getQuotedesc() {
return quotedesc;
}
}
我有一个回收器卡片视图,其中包含引号和作者......作为文本视图......和一个共享按钮......在每张卡片中......当用户想要分享报价时,特定卡片的作者......他可以将引用(字符串)与作者(字符串)一起分享给消息、whatsapp 等应用程序.我该如何解决这个问题?正在实施parcelable是正确的过程..为此目的与否..如果正确,我应该在点击共享按钮时使用什么代码
@Override
public void onBindViewHolder(final MyRecycleViewAdapter.ViewHolder myholder, int position) {
final MyQuote myQuote = myQuoteList.get(position);
myholder.tv_author.setText(myQuote.getAuthor());
myholder.tv_quote.setText(myQuote.getQuotedesc());
myholder.im_favlike.setImageResource(R.drawable.fav_border);
myholder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent (Intent.ACTION_SEND);
intent.putParcelableArrayListExtra("mani" , MyQuote);
intent.setType("text/plain");
view.getContext().startActivity(Intent.createChooser(intent,"send to"));
}
});
}
推荐答案
看看这个插件 Parcelable 代码生成器.
它将帮助您为 POJO 类自动生成可打包的样板,然后您可以按照 Abhishek 的建议直接使用它intent.putParcelableArrayListExtra(key_name, list);
It will help you to auto generate parcelable boilerplate for a POJO class and then you can directly use it as suggested by Abhishek
intent.putParcelableArrayListExtra(key_name, list);
这篇关于将 ArrayList 值传递给 Intent.putExtra for Intent.ACTION_SEND的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!