如何在回收卡视图中共享cardview的内容,如textview字符串

问题描述 投票:1回答:1

这个应用程序包含一个Recycler cardview(2 textviews)..如何使用我已经创建的共享按钮共享textview字符串..

my recycle view adapter.Java

public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.ViewHolder>{

    private List<MyQuote> myQuoteList;
    private Context mCtx;

    public MyRecycleViewAdapter(List<MyQuote> list, Context mCtx) {
        this.myQuoteList = list;
        this.mCtx = mCtx;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.sample_quotecards, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final MyRecycleViewAdapter.ViewHolder myholder, final 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);


        // share button of a recycler cardview
        myholder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


             //intent to share list values which are already defined to apps like facebook , whatsapp etc
ArrayList<String>testlist = new ArrayList<String>();
testlist.add(myQuoteList.get(position).getAuthor());
testlist.add(myQuoteList.get(position).getQuotedesc());

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, testlist);
shareIntent.setType("image/*");
view.getContext().startActivity(Intent.createChooser(shareIntent , "share this quote via"));


            }
        });
    }







    @Override
    public int getItemCount() {
        return myQuoteList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tv_author;
        public ImageView im_favlike;
        public TextView tv_quote;
        public ImageButton buttonViewOption;

        public ViewHolder(View itemView) {
            super(itemView);
             im_favlike =(ImageView)itemView.findViewById(R.id.likeimg);
            tv_author= (TextView) itemView.findViewById(R.id.author_title);
            tv_quote= (TextView) itemView.findViewById(R.id.quote_text);
            buttonViewOption = (ImageButton) itemView.findViewById(R.id.imageViewOptions);
        }
    }
}

my quote.Java

import android.os.Parcel;
import android.os.Parcelable;

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

当我使用这样的意图时,我得到以下错误。错误的第二个参数类型。找到:'java.util.ArrayList',必需:'java.util.ArrayList'少...在Intent中putParcelableArrayListExtra(String,java.util.ArrayList)不能应用于(String,java.util.ArrayList)

 public void onBindViewHolder(final MyRecycleViewAdapter.ViewHolder myholder, final 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);


        // share button of a recycler cardview
        myholder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


             //intent to share list values which are already defined to apps like facebook , whatsapp etc
ArrayList<String>testlist = new ArrayList<String>();
testlist.add(myQuoteList.get(position).getAuthor());
testlist.add(myQuoteList.get(position).getQuotedesc());

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, testlist);
shareIntent.setType("image/*");
view.getContext().startActivity(Intent.createChooser(shareIntent , "share quote via"));


            }
        });
    }
android android-recyclerview recycler-adapter android-cardview
1个回答
0
投票

如果要共享简单日期,就像文本一样,其他应用程序必须使用Intents。例如,使用此代码,您可以与其他应用程序共享文本:

Intent shareText = ShareCompat.IntentBuilder.from(activity)
  .setType("text/plain")
  .setText(shareText)
  .getIntent();
if (shareText.resolveActivity(getPackageManager()) != null) {
  startActivity(shareText);
}

但是如果你想共享像arrayList这样的多部分数据,你可以这样做:

ArrayList<String> testList = new ArrayList<String>();
testList.add(yourFirstText); // Add your text here
testList.add(yourSecondText);// Add your text here

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, testList);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share my list to.."));

更多信息可以访问here

对于您的RecyclerView,当用户点击列表中的每个项目时,您可以调用我在上面编写的代码。

然后,您可以使用该卡的项目创建ArrayList。例如,在你的onBindViewHolder中你可以设置这样的东西:

myholder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

    ArrayList<String> testList = new ArrayList<String>();
    testList.add(myQuoteList.get(position).getAuthor); // Add your text here
    testList.add(myQuoteList.get(position).getQuotedesc);// Add your text here

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
    shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, testList);
    shareIntent.setType("image/*");
    startActivity(Intent.createChooser(shareIntent, "Share my list to.."));

                    }
                });

希望这可以帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.