· 8 years ago · Mar 18, 2018, 02:14 PM
1I am making News app there is a favorite button when the user clicks to favorite button it should save an article and show it another activity I have already implemented SQLite and content provider part.
2
3below my Database code where I have implemented the database.
4
5//Sqlite creating table
6 public static final String SQL_CREATE_TABLE_ARTICLE = "CREATE TABLE IF NOT EXISTS "
7 + ArticleColumns.TABLE_NAME + " ( "
8 + ArticleColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
9 + ArticleColumns.TITLE + " TEXT NOT NULL, "
10 + ArticleColumns.TITLE_DESCRIPTION + " TEXT, "
11 + ArticleColumns.AUTHOR + " TEXT "+
12 " )";
13below News DetailActivity code where I have implemented content provider.
14
15 public class NewsDetailActivity extends AppCompatActivity {
16 ImageButton addToFavoritesBtn;
17 Article article;
18 @Override
19 protected void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.news_details);
22
23
24 Toolbar myChildToolbar =
25 (Toolbar) findViewById(R.id.detail_toolbar);
26 setSupportActionBar(myChildToolbar);
27
28 // Get a support ActionBar corresponding to this toolbar
29 ActionBar ab = getSupportActionBar();
30 ab.setDisplayHomeAsUpEnabled(true);
31
32 // Enable the Up button
33
34 getSupportActionBar().setDisplayShowTitleEnabled(false);
35
36 final Article article = (Article) getIntent().getParcelableExtra("myDataKey");
37
38 FloatingActionButton share = (FloatingActionButton)findViewById(R.id.share_fab);
39 ImageButton shareButton = (ImageButton)findViewById(R.id.share_button);
40 shareButton.setOnClickListener(new View.OnClickListener() {
41 @Override
42 public void onClick(View v) {
43 Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
44 sharingIntent.setType("text/plain");
45 String articleDescription = article.getDescription() ;
46 String articleTitle = article.getTitle();
47 sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, articleDescription);
48 sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, articleTitle);
49 startActivity(Intent.createChooser(sharingIntent, "Share using"));
50 }
51 });
52
53 share.setOnClickListener(new View.OnClickListener() {
54 @Override
55 public void onClick(View v) {
56
57 Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
58 sharingIntent.setType("text/plain");
59 String articleDescription = article.getDescription() ;
60 String articleTitle = article.getTitle();
61 sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, articleDescription);
62 sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, articleTitle);
63 startActivity(Intent.createChooser(sharingIntent, "Share using"));
64 }
65 });
66 TextView textView = (TextView) findViewById(R.id.article_title);
67 final String articleTitle = article.getTitle();
68
69 if (articleTitle != null) {
70 textView.setText(articleTitle);
71 }
72 TextView textView1 = (TextView) findViewById(R.id.article_author);
73 final String articleAuthor = article.getAuthor();
74
75
76 if (articleAuthor != null) {
77 textView1.setText(articleAuthor);
78 }
79
80
81 TextView textView2 = (TextView) findViewById(R.id.article_body);
82 String articleDescription = article.getDescription();
83 if (articleDescription != null) {
84 textView2.setText(articleDescription);
85
86 }
87 Button button = (Button) findViewById(R.id.article_url);
88 final String articleUrl = article.getUrl();
89
90
91 button.setOnClickListener(new View.OnClickListener() {
92 @Override
93 public void onClick(View arg0) {
94 Intent intent = new Intent(Intent.ACTION_VIEW,
95 Uri.parse(articleUrl));
96 startActivity(intent);
97 }
98 });
99
100 Picasso.with(this).load(article.getUrlToImage()).into((ImageView) findViewById(R.id.photo));
101
102 addToFavoritesBtn = (ImageButton) findViewById(R.id.favorite_button);
103 addToFavoritesBtn.setOnClickListener(new View.OnClickListener() {
104 @Override
105 public void onClick(View view) {
106 ContentValues values = new ContentValues();
107 // values.put(ArticleColumns._ID, "INTEGER PRIMARY KEY AUTOINCREMENT"); //Value bro example 1
108 values.put(ArticleColumns.TITLE, article.getTitle() ); // name
109 values.put(ArticleColumns.TITLE_DESCRIPTION, article.getDescription());
110 values.put(ArticleColumns.AUTHOR, article.getAuthor());
111
112 getContentResolver().insert(ArticleColumns.CONTENT_URI, values);
113 }
114 });
115 }
116
117
118
119 }