如何在android中分享帖子的内容?

时间:2017-09-02 06:27:35

标签: android mysql android-recyclerview

我有一个recycelerview,可以从mySQl数据库在线获取JSON数据,我希望每个帖子都有一个共享按钮,可以分享每个帖子的内容,因为你看到我使用了“ACTION_SEND”代码,但它没有分享我的内容并且只是分享正文句子(sharedBodyText中的确切句子),请告诉我如何分享我的帖子?这是我的代码: 显示完整帖子的页面的代码:

public class full_post extends AppCompatActivity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_post);


    Intent intent=getIntent();


    int id=intent.getIntExtra(koreDatabaseOpenHelper.COL_ID,0);



    String title=intent.getStringExtra(koreDatabaseOpenHelper.COL_TITLE);
    String content=intent.getStringExtra(koreDatabaseOpenHelper.COL_CONTENT);
    String date=intent.getStringExtra(koreDatabaseOpenHelper.COL_DATE);


    TextView titleTextView=(TextView)findViewById(R.id.post_title);
    TextView contentTextView=(TextView)findViewById(R.id.post_content);
    TextView dateTextView=(TextView)findViewById(R.id.post_date);


    titleTextView.setText(title);
    contentTextView.setText(content);
    dateTextView.setText(date);


}
public void shareText(View view) {
    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
    intent.setType("text/plain");
    String shareBodyText =(koreDatabaseOpenHelper.COL_CONTENT) ;
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject/Title");
    intent.putExtra(android.content.Intent.EXTRA_TEXT, shareBodyText);
    startActivity(Intent.createChooser(intent, "Choose sharing method"));

}}
我的DatabaseHelper的

代码:

public class koreDatabaseOpenHelper extends SQLiteOpenHelper {

private static final String TAG = "DatabaseOpenHelper";


private static final String DATABASE_NAME="db_kdramadl";
private static final int DATABASE_VERSION=1;

private static final String POST_TABLE_NAME="tbl_posts";

public static final String COL_ID="col_id";
public static final String COL_TITLE="col_title";
public static final String COL_CONTENT="col_content";
public static final String COL_DATE="col_date";

private static final String SQL_COMMAND_CREATE_POST_TABLE="CREATE TABLE IF NOT EXISTS "+POST_TABLE_NAME+"("+
        COL_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
        COL_TITLE+" TEXT,"+
        COL_CONTENT+" TEXT, "+
        " INTEGER DEFAULT 0, "+
        COL_DATE+" TEXT);";

Context context;
public koreDatabaseOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    try {
        db.execSQL(SQL_COMMAND_CREATE_POST_TABLE);
    }catch (SQLException e){
        Log.e(TAG, "onCreate: "+e.toString() );
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}


public boolean addPost(Post post){
    ContentValues cv=new ContentValues();
    cv.put(COL_ID,post.getId());
    cv.put(COL_TITLE,post.getTitle());
    cv.put(COL_CONTENT,post.getContent());
    cv.put(COL_DATE,post.getDate());

    SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
    long isInserted=sqLiteDatabase.insert(POST_TABLE_NAME,null,cv);

    Log.i(TAG, "addPost: "+isInserted);

    if (isInserted>0){
        return true;
    }else{
        return false;
    }
}

public void addPosts(List<Post> posts){
    for (int i = 0; i < posts.size(); i++) {
        if (!checkPostExists(posts.get(i).getId())) {
            addPost(posts.get(i));
        }
    }
}

这是我的适配器:

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {
private Context context;
private List<Post> posts;

public PostAdapter (Context context, List<Post> posts){
    this.context = context;
    this.posts = posts;
}
@Override
public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(context).inflate(R.layout.postha,parent,false);
    Typeface morvaridTypeface=Typeface.createFromAsset(context.getAssets(),"fonts/morvarid.ttf");
    return new PostViewHolder(view,morvaridTypeface);
}

@Override
public void onBindViewHolder(PostViewHolder holder, int position) {
   final Post post=posts.get(position);


    holder.title.setText(post.getTitle());
    holder.content.setText(post.getContent());
    holder.date.setText(post.getDate());


    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(context,full_post.class);
            intent.putExtra(koreDatabaseOpenHelper.COL_ID,post.getId());
            intent.putExtra(koreDatabaseOpenHelper.COL_TITLE,post.getTitle());
            intent.putExtra(koreDatabaseOpenHelper.COL_CONTENT,post.getContent());
            intent.putExtra(koreDatabaseOpenHelper.COL_DATE,post.getDate());
            context.startActivity(intent);
        }
    });
}

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

public class PostViewHolder extends RecyclerView.ViewHolder{
    private TextView title;
    private TextView content;
    private TextView date;

    public PostViewHolder(View itemView) {
        super(itemView);
        title=(TextView)itemView.findViewById(R.id.post_title);


        content=(TextView)itemView.findViewById(R.id.post_content);


        date=(TextView)itemView.findViewById(R.id.post_date);

    }
}}

2 个答案:

答案 0 :(得分:2)

我使用下面的代码来分享帖子网址。您可以使用mUrl作为内容。

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,**mUrl**);
startActivity(Intent.createChooser(intent,getString("your apps title")));

mUrl (字符串)替换为您的帖子内容(字符串)......

答案 1 :(得分:1)

full_post.class中,接收从适配器传递的额外内容,然后创建一个按钮作为共享,然后添加此

 share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent what = new Intent();
            what.setAction(Intent.ACTION_SEND);
            what.putExtra(Intent.EXTRA_TEXT, "Post Id:" + postId+ "\nPost 
            Title:" +postTitle);
            what.setType("text/plain");
            startActivity(Intent.createChooser(what, "Share with"));
        }
    });

其中postId,postTitle是从适配器类

收到的axtra