【发布时间】: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