android查询数据库中新闻表的信息并将所有信息显示在界面的listview上 所有的相关代码
时间: 2024-02-13 12:00:49 浏览: 63
好的,以下是一个简单的 Android 查询数据库中新闻表的信息并将所有信息显示在界面的 ListView 上的代码示例:
首先,在你的 Android 项目中创建一个数据库帮助类 NewsDBHelper.java,用于创建和管理数据库:
```java
public class NewsDBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "news.db";
private static final int DATABASE_VERSION = 1;
public NewsDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS news ("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "title TEXT,"
+ "content TEXT,"
+ "date TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS news");
onCreate(db);
}
}
```
接下来,在你的 Activity 中,你可以使用以下代码查询数据库中的新闻信息,并将它们显示在 ListView 中:
```java
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private NewsDBHelper mDBHelper;
private SQLiteDatabase mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = findViewById(R.id.listView);
mDBHelper = new NewsDBHelper(this);
mDatabase = mDBHelper.getWritableDatabase();
Cursor cursor = mDatabase.query("news", null, null, null, null, null, null);
List<News> newsList = new ArrayList<>();
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 date = cursor.getString(cursor.getColumnIndex("date"));
newsList.add(new News(id, title, content, date));
}
cursor.close();
NewsAdapter adapter = new NewsAdapter(this, newsList);
mListView.setAdapter(adapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
mDatabase.close();
mDBHelper.close();
}
private static class NewsAdapter extends ArrayAdapter<News> {
private LayoutInflater mInflater;
public NewsAdapter(Context context, List<News> newsList) {
super(context, 0, newsList);
mInflater = LayoutInflater.from(context);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_news, parent, false);
}
News news = getItem(position);
TextView titleView = convertView.findViewById(R.id.title);
TextView contentView = convertView.findViewById(R.id.content);
TextView dateView = convertView.findViewById(R.id.date);
titleView.setText(news.getTitle());
contentView.setText(news.getContent());
dateView.setText(news.getDate());
return convertView;
}
}
}
```
其中,News.java 是一个简单的 Java Bean 类,用于存储新闻数据:
```java
public class News {
private int id;
private String title;
private String content;
private String date;
public News(int id, String title, String content, String date) {
this.id = id;
this.title = title;
this.content = content;
this.date = date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
```
最后,你需要在你的布局文件中定义 ListView 和每个列表项的布局:
activity_main.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
```
list_item_news.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<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="16dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp" />
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" />
<TextView
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textColor="#999999" />
</LinearLayout>
```
以上就是一个简单的 Android 查询数据库中新闻表的信息并将所有信息显示在界面的 ListView 上的代码示例,希望对你有所帮助。
阅读全文