Passing ArrayList value to a Intent.putExtra for Intent.ACTION_SEND

本小妞迷上赌 提交于 2019-12-24 17:29:06

问题


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"));

            }
        });
    }

回答1:


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




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/52347734/passing-arraylist-value-to-a-intent-putextra-for-intent-action-send

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!