Android 开发新闻系统:使用 SQLite 数据库保存新闻数据
在 Android 开发中,可以使用 SQLite 数据库来保存新闻数据。下面是一个简单的布局示例,用于展示新闻列表和保存新闻到数据库。
- 首先,在布局文件中创建一个 RecyclerView,用于显示新闻列表。在 activity_main.xml 文件中添加以下代码:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
- 创建一个新闻项的布局文件,例如 news_item.xml,用于显示每条新闻的标题和内容。在该文件中添加以下代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/contentTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textSize="14sp" />
</LinearLayout>
- 创建一个新闻数据类,例如 News.java,用于保存新闻的标题和内容。在该类中添加以下代码:
public class News {
private String title;
private String content;
public News(String title, String content) {
this.title = title;
this.content = content;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
}
- 创建一个适配器类,例如 NewsAdapter.java,用于将新闻数据绑定到 RecyclerView 中的每个新闻项。在该类中添加以下代码:
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private List<News> newsList;
public NewsAdapter(List<News> newsList) {
this.newsList = newsList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
News news = newsList.get(position);
holder.titleTextView.setText(news.getTitle());
holder.contentTextView.setText(news.getContent());
}
@Override
public int getItemCount() {
return newsList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView titleTextView;
TextView contentTextView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.titleTextView);
contentTextView = itemView.findViewById(R.id.contentTextView);
}
}
}
- 在 MainActivity.java 中,使用 SQLite 数据库保存新闻数据。以下是一个简单的示例代码:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private NewsAdapter newsAdapter;
private List<News> newsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
newsList = new ArrayList<>();
newsAdapter = new NewsAdapter(newsList);
recyclerView.setAdapter(newsAdapter);
// 保存新闻到数据库
saveNewsToDatabase();
}
private void saveNewsToDatabase() {
// 创建 SQLite 数据库
SQLiteDatabase db = openOrCreateDatabase('NewsDB', Context.MODE_PRIVATE, null);
// 创建新闻表
db.execSQL('CREATE TABLE IF NOT EXISTS news (title TEXT, content TEXT)');
// 插入新闻数据
db.execSQL('INSERT INTO news VALUES ('News Title 1', 'News Content 1')');
db.execSQL('INSERT INTO news VALUES ('News Title 2', 'News Content 2')');
// 查询新闻数据
Cursor cursor = db.rawQuery('SELECT * FROM news', null);
if (cursor.moveToFirst()) {
do {
String title = cursor.getString(0);
String content = cursor.getString(1);
News news = new News(title, content);
newsList.add(news);
} while (cursor.moveToNext());
}
cursor.close();
// 更新 RecyclerView
newsAdapter.notifyDataSetChanged();
// 关闭数据库连接
db.close();
}
}
以上是一个简单的布局示例,用于展示新闻列表和保存新闻到 SQLite 数据库。你可以根据实际需求进行修改和扩展。
原文地址: http://www.cveoy.top/t/topic/f330 著作权归作者所有。请勿转载和采集!