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

时间:2018-09-15 16:16:57

标签: android android-recyclerview recycler-adapter android-cardview

此应用程序包含一个回收站cardview(2个textviews)..如何使用已创建的共享按钮共享textview字符串。

MyRecycleViewAdapter.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);
        }
    }
}

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

im在使用此类意图时出现以下错误。 第二个参数类型错误。找到:'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"));


            }
        });
    }

1 个答案:

答案 0 :(得分:0)

如果要与其他应用程序共享文本等简单日期,则必须使用Intent。 例如,使用此代码,您可以与其他应用程序共享文本:

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

                    }
                });

希望这可以为您提供帮助。