Passing ArrayList value to a Intent.putExtra for Intent.ACTION_SEND
How to pass arraylist value (samplename, samplequote ) to the intent.putExtra.. for sharing the text..
In MainActivity.java
list = new ArrayList<>(); //loading list view item with this function loadRecyclerViewItem(); } private void loadRecyclerViewItem() { list.add(new MyQuote("sample name","sample quote"));
In 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")); } }); }
EDIT after implementing Parcelable in MyQuote class.. like this when i use intent.putParcelableArrayListExtra("mani" , MyQuote);.... i'm getting "expression expected ...in 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; } }
I have a recycler cardview which contains quotes,and authors.. as textviews..and a sharebutton.. in each card.. when user want to share the quote, author of a particular card.. he can share the quote(string) along with author(string) to apps like messeges, whatsapp ..etc.. how can i solve this ? is implementing parcelable is correct process..for this purpose or not..if it is correct what code should i use in onclick of a sharebutton
@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")); } }); }
You have to implement Parcelable interface in MyQuote Class and then you can send it through intent like this :-
intent.putParcelableArrayListExtra(key_name,your_list);
Passing ArrayList value to a Intent.putExtra for Intent.ACTION_SEND, You have to implement Parcelable interface in MyQuote Class and then you can send it through intent like this :- intent. We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents. We’ll see an example of storing a string in this Intent object using a key.
The class of your array elements should implement Parcelable
interface.
After doing so, you can use Intent.putParcelableArrayListExtra(ARRAY_LIST_KEY,fooArrayList)
to send the array list and then Intent.getStringArrayListExtra(ARRAY_LIST_KEY)
to retrieve the list
Using putExtra() and getExtras() in android, . It has two parameters, first one specifies the name which of the extra data,and the second parameter is the data itself. Assuming that your List is a list of strings make data an ArrayList<String> and use intent.putStringArrayListExtra("data", data) Here is a skeleton of the code you need: Declare List. private List<String> test; Init List at appropriate place. test = new ArrayList<String>(); and add data as appropriate to test. Pass to intent as follows:
Hi check out this plugin Parcelable Code Generator.
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);
How do I get extra data from intent on Android?, () Return the intent that started this activity. If you start an Activity with some data, for example by doing. Intent intent = new Intent(context, SomeActivity. class); intent. Intent.putExtra() takes in a Bundle though, so you would need to do something more like this: Bundle bundle = new Bundle(); bundle.putStringArray("arrayLabel",(String[])arrayList.toArray()); intent.putExtra("extrasBundle",bundle); Then inside the Activity that will receive the ArrayList: Bundle bundle = intent.getBundleExtra("extrasBundle"); ArrayList list = Arrays.asList(bundle.getStringArray("arrayLabel")); – BigFwoosh Apr 29 '11 at 18:19
Understand Parcelable In Android - Nikhil Dhyani, intent.putExtra("url", "url of the file to download"); intent.putExtra("receiver", new DownloadReceiver(new Handler())); ACTION_SEND); intent. to "send multiple" to get more than one attachment final Intent emailIntent = new Intent(Intent. EXTRA_TEXT, emailText); //has to be an ArrayList ArrayList<Uri> uris = new Code for passing multiple data or values between activities in Android : Method 1 : Using Intent to pass data and Bundle to extract data between activities in Android ActivityOne.java
Android : When do we use getIntent()?, An Intent is a small message passed around the Android system. void startActivityForResult (Intent intent, int requestCode, Bundle options); Intent putExtra(String name, double[] value); Intent putExtra(String name, int value) Arraylist of Serializable object: same as single object passing ACTION_SEND instead of Intent. You can pass an ArrayList<E> the same way, if the E type is Serializable. You would call the putExtra (String name, Serializable value) of Intent to store, and getSerializableExtra (String name) for retrieval.
android.content.Intent.putExtra java code examples, The data type vnd.android.cursor.item/vnd.google.note is a URI from which a Cursor An ArrayList of String annotations describing content for ACTION_CHOOSER . A constant String that is associated with the Intent, used with ACTION_SEND to If set in an Intent passed to Context#startActivity , this flag will cause any In this technique, we have used the Serialization for the list passing. Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed.
Comments
- I have a recycler cardview which contains quotes,and authors.. as textviews..and a sharebutton.. in each card.. when user want to share the quote, author of a particular card.. he can share the quote(string) along with author(string) to apps like messeges, whatsapp ..etc.. how can i solve this ? is implementing parcelable is correct process..for this purpose or not..if it is correct what code should i use in onclick of a sharebutton
- I would recommend concatenating the author and quote name into one formatted string then sending it using putExtra(Intent.EXTRA_TEXT, textToShare), this is if you want to share a single quote