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

问题描述:

我有一个recycelerview即得到MySQL数据库JSON数据在线,我想有可共享每篇文章的内容,当你看到我用“ACTION_SEND”代码每篇文章一个分享按钮,但不同意我的内容和?只是共享体句子(即在sharedBodyText确切的句子),请告诉我,我怎么能分享我的帖子在这里是我的代码:该页面 代码显示完整的文章:的如何在android中分享帖子的内容?

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(字符串)与您帖子内容(串)替换...

+0

那应该怎么辨别mUrl?(原因已经有错误),对不起,我是android的新,所以有很多的事情,我不知道 –

+0

mUrl从我的代码删除,并把你的帖子... –

+0

我做这和你在那个位置知道刚才我们可以把字符串,所以我想我的TextView转换为字符串,但它的主要错误,HTTP://uupload.ir/files/0bbs_casting.png –

在你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")); 
     } 
    }); 

哪里帖子ID,postTitle被接收为axtra从适配器类

+0

你的意思是我应该这样做? http://uupload.ir/files/4z29_staaaaaa.png –

+0

让你的帖子ID喜欢这个帖子ID = getIntent()getStringExtra(koreDatabaseOpenHelper.COL_ID)。 –

+0

更好您删除功能,在把代码你的onCreate –