如何用android studio实现新闻收藏
时间: 2023-07-12 20:43:22 浏览: 156
Android Studio实现一个新闻APP,功能巨多,大作业必备项目
5星 · 资源好评率100%
以下是一些实现新闻收藏的基本步骤:
1. 创建一个新的数据库类,用于创建和管理 SQLite 数据库和表。在该类中定义表和字段,以及增删改查等操作方法。
```
public class NewsDBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "news.db";
private static final int DB_VERSION = 1;
public NewsDBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS news " +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"title TEXT, " +
"content TEXT, " +
"url TEXT, " +
"image_url TEXT, " +
"is_favorite INTEGER DEFAULT 0)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Upgrade database if necessary
}
public void addNews(News news) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", news.getTitle());
values.put("content", news.getContent());
values.put("url", news.getUrl());
values.put("image_url", news.getImageUrl());
db.insert("news", null, values);
db.close();
}
public void removeNews(int id) {
SQLiteDatabase db = getWritableDatabase();
db.delete("news", "_id=?", new String[]{String.valueOf(id)});
db.close();
}
public void updateNews(News news) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", news.getTitle());
values.put("content", news.getContent());
values.put("url", news.getUrl());
values.put("image_url", news.getImageUrl());
db.update("news", values, "_id=?", new String[]{String.valueOf(news.getId())});
db.close();
}
public List<News> getFavoriteNews() {
List<News> newsList = new ArrayList<>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query("news", null, "is_favorite=?", new String[]{"1"}, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String content = cursor.getString(cursor.getColumnIndex("content"));
String url = cursor.getString(cursor.getColumnIndex("url"));
String imageUrl = cursor.getString(cursor.getColumnIndex("image_url"));
int isFavorite = cursor.getInt(cursor.getColumnIndex("is_favorite"));
News news = new News(id, title, content, url, imageUrl, isFavorite == 1);
newsList.add(news);
}
cursor.close();
}
db.close();
return newsList;
}
public void setNewsFavorite(int id, boolean isFavorite) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("is_favorite", isFavorite ? 1 : 0);
db.update("news", values, "_id=?", new String[]{String.valueOf(id)});
db.close();
}
}
```
2. 在新闻列表界面中,显示新闻列表,并添加一个收藏按钮。当用户点击该按钮时,将该新闻的 ID 存入数据库中,并更新按钮的状态。
```
public class NewsListActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private NewsListAdapter mAdapter;
private NewsDBHelper mDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_list);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new NewsListAdapter(this);
mRecyclerView.setAdapter(mAdapter);
mDBHelper = new NewsDBHelper(this);
List<News> newsList = getNewsList();
mAdapter.setNewsList(newsList);
}
private List<News> getNewsList() {
// Get news list from API or other data source
// ...
}
public void onFavoriteClick(int position) {
News news = mAdapter.getNewsList().get(position);
boolean isFavorite = !news.isFavorite();
news.setFavorite(isFavorite);
mDBHelper.setNewsFavorite(news.getId(), isFavorite);
mAdapter.notifyItemChanged(position);
}
}
```
3. 在新闻详情界面中,显示新闻内容,并添加一个收藏按钮。当用户点击该按钮时,将该新闻的 ID 存入数据库中,并更新按钮的状态。
```
public class NewsDetailActivity extends AppCompatActivity {
private News mNews;
private NewsDBHelper mDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_detail);
mNews = getIntent().getParcelableExtra("news");
mDBHelper = new NewsDBHelper(this);
TextView titleTextView = findViewById(R.id.title_text_view);
TextView contentTextView = findViewById(R.id.content_text_view);
ImageView imageView = findViewById(R.id.image_view);
Button favoriteButton = findViewById(R.id.favorite_button);
titleTextView.setText(mNews.getTitle());
contentTextView.setText(mNews.getContent());
Glide.with(this).load(mNews.getImageUrl()).into(imageView);
boolean isFavorite = mNews.isFavorite();
favoriteButton.setText(isFavorite ? "取消收藏" : "收藏");
favoriteButton.setOnClickListener(v -> {
isFavorite = !isFavorite;
mNews.setFavorite(isFavorite);
mDBHelper.setNewsFavorite(mNews.getId(), isFavorite);
favoriteButton.setText(isFavorite ? "取消收藏" : "收藏");
});
}
}
```
4. 在新闻收藏列表界面中,加载已经收藏的新闻列表,并显示在列表中。
```
public class FavoriteListActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private NewsListAdapter mAdapter;
private NewsDBHelper mDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite_list);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new NewsListAdapter(this);
mRecyclerView.setAdapter(mAdapter);
mDBHelper = new NewsDBHelper(this);
List<News> newsList = mDBHelper.getFavoriteNews();
mAdapter.setNewsList(newsList);
}
}
```
以上代码仅供参考,具体实现可以根据自己的需要进行调整和优化。
阅读全文