为什么按意图发送内容不起作用

Why sending content by intent does not work

我有一个 activity,它在 recyclerview 中包含许多 post,每个 post 都放在 cardView 中(post 从我的数据库中获取主持人)。我想使用发送意图来分享 post 中的任何一个。我的分享按钮可以用,但工作方式不对。

例如,当我点击分享第一个 post 时,它分享了第三个 post!当我点击分享第七个 post 时,它分享第十个 post 等等。

有什么问题吗?有人可以帮忙吗?

这是我的xml:

<ImageButton
        android:id="@+id/send_post"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:onClick="sharedtextClicked"
        android:src="@color/colorPrimary" />

这是我的分享代码:

public void sharedtextClicked(View view) {
    String txt=((TextView)findViewById(R.id.Postcontent)).getText().toString();
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT,txt);
    sendIntent.setType("text/plain");
    startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}

这是我的适配器:

    View view = LayoutInflater.from(context).inflate(R.layout.layout_post,parent,false);
    Typeface arialTypeface=Typeface.createFromAsset(context.getAssets(),"fonts/arial.ttf");
    return new PostviewHolder(view,arialTypeface);
}

@Override
public void onBindViewHolder(PostviewHolder holder, int position) {

    Post post=posts.get(position);
    holder.content.setText(post.getContent());
}

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

public class PostviewHolder extends RecyclerView.ViewHolder{

    private TextView content;

    public PostviewHolder(View itemView, Typeface arialTypeface) {
        super(itemView);
        content=(TextView)itemView.findViewById(R.id.Postcontent);
        content.setTypeface(arialTypeface);
    }
}

这是layout_post:

<?xml version="1.0" encoding="utf-8"?>
 <android.support.v7.widget.CardView 
  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardCornerRadius="9dp"
app:cardElevation="2dp">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="4dp">
 <TextView
        android:id="@+id/Postcontent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_margin="8dp"
        android:textColor="#000"
        android:textSize="20sp"
         />
   <ImageButton
        android:id="@+id/send_post"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:onClick="sharedtextClicked"
        android:src="@color/colorPrimary" />
 </LinearLayout>
 </android.support.v7.widget.CardView>

我觉得你的逻辑不对。试试这个

layout_post.xml 文件

中删除 android:onClick="sharedtextClicked"

删除sharedtextClicked方法。

更改 您在适配器中的代码:

@Override
public void onBindViewHolder(@NonNull final PostviewHolder holder, int position) {
    holder.content.setText(post.get(holder.getAdapterPosition()).getContent());
    holder.sendPost.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, post.get(holder.getAdapterPosition()).getContent());
            sendIntent.setType("text/plain");
            Context context = view.getRootView().getContext();
            context.startActivity(Intent.createChooser(sendIntent, context.getResources().getText(R.string.send_to)));
        }
    });
}

public class PostviewHolder extends RecyclerView.ViewHolder {

        private TextView content;
        private ImageButton sendPost;

        public PostviewHolder(View itemView, Typeface arialTypeface) {
            super(itemView);
            content = (TextView) itemView.findViewById(R.id.Postcontent);
            sendPost = (ImageButton) itemView.findViewById(R.id.send_post);
            content.setTypeface(arialTypeface);
        }
    }