用android studio实现新闻收藏
时间: 2023-07-12 08:47:58 浏览: 230
好的,下面我来详细介绍一下如何用Android Studio实现新闻收藏功能。
1. 创建SQLite数据库
首先在Android Studio中创建一个新的Android工程,然后新建一个类用于创建SQLite数据库,并且定义好存储新闻的表结构。在这个类中,需要继承SQLiteOpenHelper类,并且重写onCreate()和onUpgrade()方法。onCreate()方法用于创建数据库和表,onUpgrade()方法用于升级数据库版本。
以下是一个示例的SQLiteOpenHelper类:
```java
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "news.db"; // 数据库名
private static final int DB_VERSION = 1; // 数据库版本号
// 新闻表
private static final String TABLE_NEWS = "news";
private static final String COLUMN_ID = "id";
private static final String COLUMN_TITLE = "title";
private static final String COLUMN_CONTENT = "content";
private static final String COLUMN_URL = "url";
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NEWS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY,"
+ COLUMN_TITLE + " TEXT,"
+ COLUMN_CONTENT + " TEXT,"
+ COLUMN_URL + " TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NEWS);
onCreate(db);
}
}
```
2. 新闻收藏
在新闻详情页面中,添加一个收藏按钮,并且为这个按钮添加一个点击事件处理方法。在这个事件处理方法中,将新闻的标题、内容和URL等信息存储到SQLite数据库中。
以下是一个示例的新闻详情页面:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceLarge"
android:text="News Title"
android:padding="16dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/news_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:text="News Content"
android:padding="16dp"/>
<Button
android:id="@+id/favorite_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="收藏"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"/>
</RelativeLayout>
```
在新闻详情页面的Java代码中,为收藏按钮添加一个点击事件处理方法,将新闻的标题、内容和URL等信息存储到SQLite数据库中。以下是一个示例的Java代码:
```java
public class NewsDetailActivity extends AppCompatActivity {
private String newsTitle;
private String newsContent;
private String newsUrl;
private Button favoriteButton;
private DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_detail);
dbHelper = new DBHelper(this);
newsTitle = getIntent().getStringExtra("title");
newsContent = getIntent().getStringExtra("content");
newsUrl = getIntent().getStringExtra("url");
TextView newsTitleView = (TextView) findViewById(R.id.news_title);
newsTitleView.setText(newsTitle);
TextView newsContentView = (TextView) findViewById(R.id.news_content);
newsContentView.setText(newsContent);
favoriteButton = (Button) findViewById(R.id.favorite_button);
favoriteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBHelper.COLUMN_TITLE, newsTitle);
values.put(DBHelper.COLUMN_CONTENT, newsContent);
values.put(DBHelper.COLUMN_URL, newsUrl);
db.insert(DBHelper.TABLE_NEWS, null, values);
Toast.makeText(getApplicationContext(), "收藏成功", Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
dbHelper.close();
}
}
```
3. 我的收藏
在应用中添加一个“我的收藏”页面,用于显示用户已经收藏的新闻列表。可以使用RecyclerView控件展示收藏的新闻列表,并且在列表中添加一个删除按钮,用于删除不需要的收藏。
以下是一个示例的“我的收藏”页面的布局文件:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/news_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</RelativeLayout>
```
在Java代码中,使用SQLite数据库查询用户已经收藏的新闻,并且将这些新闻展示在RecyclerView中。在RecyclerView的适配器中,为列表项添加一个删除按钮,并且为这个按钮添加一个点击事件处理方法。
以下是一个示例的“我的收藏”页面的Java代码:
```java
public class FavoriteActivity extends AppCompatActivity implements NewsAdapter.OnItemClickListener {
private RecyclerView newsList;
private NewsAdapter newsAdapter;
private List<News> newsData = new ArrayList<>();
private DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite);
dbHelper = new DBHelper(this);
newsList = (RecyclerView) findViewById(R.id.news_list);
newsList.setLayoutManager(new LinearLayoutManager(this));
newsAdapter = new NewsAdapter(newsData);
newsAdapter.setOnItemClickListener(this);
newsList.setAdapter(newsAdapter);
updateNewsList();
}
private void updateNewsList() {
newsData.clear();
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(DBHelper.TABLE_NEWS, null, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
News news = new News();
news.setTitle(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_TITLE)));
news.setContent(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_CONTENT)));
news.setUrl(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_URL)));
newsData.add(news);
} while (cursor.moveToNext());
}
cursor.close();
newsAdapter.notifyDataSetChanged();
}
@Override
public void onItemClick(View view, int position) {
News news = newsData.get(position);
Intent intent = new Intent(this, NewsDetailActivity.class);
intent.putExtra("title", news.getTitle());
intent.putExtra("content", news.getContent());
intent.putExtra("url", news.getUrl());
startActivity(intent);
}
@Override
public void onItemLongClick(View view, final int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setMessage("确定要删除这条收藏吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
News news = newsData.get(position);
db.delete(DBHelper.TABLE_NEWS, DBHelper.COLUMN_TITLE + "=?", new String[]{news.getTitle()});
newsData.remove(position);
newsAdapter.notifyItemRemoved(position);
}
});
builder.setNegativeButton("取消", null);
builder.show();
}
@Override
protected void onDestroy() {
super.onDestroy();
dbHelper.close();
}
}
```
在适配器NewsAdapter中,为列表项添加一个删除按钮,并且为这个按钮添加一个点击事件处理方法。以下是一个示例的适配器代码:
```java
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private List<News> newsData;
private OnItemClickListener onItemClickListener;
public NewsAdapter(List<News> newsData) {
this.newsData = newsData;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView newsTitleView;
TextView newsContentView;
Button deleteButton;
public ViewHolder(View itemView) {
super(itemView);
newsTitleView = (TextView) itemView.findViewById(R.id.news_title);
newsContentView = (TextView) itemView.findViewById(R.id.news_content);
deleteButton = (Button) itemView.findViewById(R.id.delete_button);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
final ViewHolder holder = new ViewHolder(view);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
if (onItemClickListener != null) {
onItemClickListener.onItemClick(holder.itemView, position);
}
}
});
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
if (onItemClickListener != null) {
onItemClickListener.onItemLongClick(holder.itemView, position);
}
}
});
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
News news = newsData.get(position);
holder.newsTitleView.setText(news.getTitle());
holder.newsContentView.setText(news.getContent());
}
@Override
public int getItemCount() {
return newsData.size();
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
}
```
至此,我们完成了新闻收藏功能的实现。用户可以在新闻详情页面中点击收藏按钮,将新闻保存到SQLite数据库中。在“我的收藏”页面中,用户可以查看已经收藏的新闻,并且可以删除不需要的收藏。
阅读全文