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

标签 android mysql android-recyclerview

我有一个从 mySQl 数据库在线获取 JSON 数据的 recycelerview,我希望每个帖子都有一个共享按钮,可以共享每个帖子的内容,正如你所看到的,我使用了“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);

    }
}}

最佳答案

我使用下面的代码来分享帖子 URL。您可以使用 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(字符串)替换为您的帖子内容(字符串)...

关于android - 如何在android中分享帖子的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46011114/

相关文章:

android - 处理程序每​​ 5 秒运行一次任务 Kotlin

java - 致命异常主要在 onclick

MySQL - 按父位置和子位置排序

mySQL GROUP_CONCAT IN JOIN

java - 获取值(value)并关闭与 recyclerview 和自定义适配器的对话框

Android:使用 Parcerable 将自定义对象的 ArrayList 从 fragment 发送到 Activity

android - 在 Media Player 中停止循环音频会出错

mysql - sql中查找不同表中的同一列

java - 如何在 Recyclerview 中加载更多的 JSON 数据

java - Android Espresso 测试在物理设备上失败(PerformException)