将ArrayList值传递给Intent.ACTION_SEND的Intent.putExtra

时间:2018-09-15 18:36:38

标签: android android-intent arraylist share-button

如何将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;
    }
}

我有一个回收站卡片视图,其中包含引号和作者(如文本视图)和每个按钮中的共享按钮。当用户要共享报价时,特定卡片的作者可以与诸如messeges,whatsapp..etc等应用程序共享报价(string)和author(string)。 我该如何解决?正在实施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"));

            }
        });
    }

3 个答案:

答案 0 :(得分:0)

您必须在MyQuote类中实现Parcelable接口,然后才能通过这样的意图将其发送:-

intent.putParcelableArrayListExtra(key_name,your_list);

答案 1 :(得分:0)

您的数组元素的类应实现Parcelable接口。 之后,您可以使用Intent.putParcelableArrayListExtra(ARRAY_LIST_KEY,fooArrayList)发送数组列表,然后使用Intent.getStringArrayListExtra(ARRAY_LIST_KEY)检索列表

答案 2 :(得分:0)

您好,请查看此插件Parcelable Code Generator

它将帮助您自动为POJO类生成可打包样板,然后您可以按照Abhishek的建议直接使用它 intent.putParcelableArrayListExtra(key_name, list);