【问题标题】:Why sending content by intent does not work为什么按意图发送内容不起作用
【发布时间】:2018-09-01 14:16:28
【问题描述】:

我有一个活动,其中包含一个 recyclerview 中的许多帖子,每个帖子都放在一个 cardView 中(帖子来自我主机中的数据库)。我想使用发送意图来分享任何帖子。我的分享按钮可以使用,但使用方式有误。

例如,当我点击分享我的第一个帖子时,它会分享第三个帖子!当我点击分享第七个帖子时,它会分享第十个帖子,依此类推。

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

这是我的 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>

【问题讨论】:

  • 你能显示你的适配器代码吗?
  • @MichaelMontero 我放了我的适配器,检查一下
  • 显示你调用shareTextClicked方法的代码。
  • @SonTruong 我不叫它。如您所见,这是我在 xml 文件中使用的 onClick 功能的方法

标签: android android-intent android-recyclerview android-cardview


【解决方案1】:

我认为你的逻辑不正确。试试这个

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

【讨论】:

  • 我做到了,但没有任何改变,也对我的问题没有帮助
  • 我明白了。可以分享layout_post.xml文件吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
  • 1970-01-01
  • 2015-10-23
相关资源
最近更新 更多