Как обмениваться содержимым карты, например текстовой строкой, в виде карты переработчика - PullRequest
0 голосов
/ 15 сентября 2018

Это приложение содержит просмотр карты переработчика (2 просмотра текста) .. как я могу поделиться строками просмотра текста с помощью кнопки «Поделиться», которую я уже создал ..

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

я получаю следующую ошибку, когда я использую намерение, как это .. Неверный 2-й тип аргумента. Найдено: 'java.util.ArrayList', обязательно: 'java.util.ArrayList' less ... putParcelableArrayListExtra (String, java.util.ArrayList) в Intent нельзя применить к (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 голосов
/ 16 сентября 2018

Если вы хотите поделиться простой датой, такой как текст, с другими приложениями, используйте 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.."));

Дополнительная информация может быть доступна здесь

Для вашего 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.."));

                    }
                });

Надеюсь, это поможет вам.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...