Android查询数据库表中所有的内容 并显示在listview组件的界面
时间: 2023-08-01 14:06:02 浏览: 89
首先,你需要创建一个数据库帮助类来处理数据库操作。这个类应该继承自 SQLiteOpenHelper 类,然后实现 onCreate() 和 onUpgrade() 方法。在 onCreate() 方法中,你应该创建你的数据库表。在 onUpgrade() 方法中,你应该处理升级数据库的操作。
接下来,你需要创建一个数据模型类来表示你的数据表中的每一行数据。这个类应该包含一个构造函数和 getter/setter 方法来获取和设置每个列的值。
然后,你需要创建一个适配器类来处理将数据显示在 ListView 中。这个适配器应该继承自 BaseAdapter 类,并实现 getCount()、getItem() 和 getView() 方法。在 getView() 方法中,你应该为每个列表项创建一个视图,并将数据填充到这个视图中。
最后,你需要在你的 Activity 中使用这个适配器来显示数据。你可以使用 findViewById() 方法获取 ListView 组件,并将适配器设置为它的适配器。然后调用适配器的 notifyDataSetChanged() 方法来刷新列表视图。
以下是一个示例代码,用于从数据库中获取数据并将其显示在 ListView 中:
```java
public class MainActivity extends AppCompatActivity {
private ListView listView;
private MyDatabaseHelper dbHelper;
private List<MyDataModel> dataList;
private MyListAdapter listAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyDatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
dataList = new ArrayList<MyDataModel>();
String[] columns = {"col1", "col2", "col3"};
Cursor cursor = db.query("my_table", columns, null, null, null, null, null);
while (cursor.moveToNext()) {
String col1 = cursor.getString(cursor.getColumnIndex("col1"));
String col2 = cursor.getString(cursor.getColumnIndex("col2"));
String col3 = cursor.getString(cursor.getColumnIndex("col3"));
MyDataModel data = new MyDataModel(col1, col2, col3);
dataList.add(data);
}
cursor.close();
listView = (ListView) findViewById(R.id.list_view);
listAdapter = new MyListAdapter(this, dataList);
listView.setAdapter(listAdapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
dbHelper.close();
}
private class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "my_db";
private static final int DB_VERSION = 1;
private static final String CREATE_TABLE = "CREATE TABLE my_table (col1 TEXT, col2 TEXT, col3 TEXT)";
public MyDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// handle database upgrade
}
}
private class MyDataModel {
private String col1;
private String col2;
private String col3;
public MyDataModel(String col1, String col2, String col3) {
this.col1 = col1;
this.col2 = col2;
this.col3 = col3;
}
public String getCol1() {
return col1;
}
public void setCol1(String col1) {
this.col1 = col1;
}
public String getCol2() {
return col2;
}
public void setCol2(String col2) {
this.col2 = col2;
}
public String getCol3() {
return col3;
}
public void setCol3(String col3) {
this.col3 = col3;
}
}
private class MyListAdapter extends BaseAdapter {
private Context context;
private List<MyDataModel> dataList;
public MyListAdapter(Context context, List<MyDataModel> dataList) {
this.context = context;
this.dataList = dataList;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item, null);
}
MyDataModel data = dataList.get(position);
TextView col1TextView = (TextView) view.findViewById(R.id.col1_text_view);
TextView col2TextView = (TextView) view.findViewById(R.id.col2_text_view);
TextView col3TextView = (TextView) view.findViewById(R.id.col3_text_view);
col1TextView.setText(data.getCol1());
col2TextView.setText(data.getCol2());
col3TextView.setText(data.getCol3());
return view;
}
}
}
```
阅读全文