我已经在互联网上搜索了,我不明白我需要做什么才能将数据从数据库显示到ListView。他们有教程,但我不太明白。 这是我的数据库处理程序代码
package com.example.databasetest;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class DBHandler {
public static final String TABLE_NAME = "tableKo";
public static final String DATABASE_NAME = "databaseKo";
private static final int DATABASE_VERSION = 1;
private static final String TAG = "DBHandler";
public static final String COL_ID = "_id";
public static final String COL_NAME = "name";
public static final String COL_ADDRESS = "address";
public static final String COL_PHONE = "phone";
public static final String COL_EMAIL = "email";
private final Context context;
private SQLiteDatabase db;
private MySQLiteOpenHelper DBHelper;
private String[] data;
private static final String CREATE_DATABASE ="create table "
+ TABLE_NAME + "(" + COL_ID
+ " integer primary key, " + COL_NAME
+ " text not null, " + COL_ADDRESS + " text not null,"
+ COL_PHONE + " text not null," + COL_EMAIL + " text not null);";
public DBHandler(Context ctx) {
this.context = ctx;
DBHelper = new MySQLiteOpenHelper(context);
}
private static class MySQLiteOpenHelper extends SQLiteOpenHelper{
public MySQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
db.execSQL(CREATE_DATABASE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, oldVersion + " to " + newVersion
+ ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
public DBHandler open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
public void insertData (String name, String address, String phone, String email) {
open();
ContentValues values = new ContentValues();
values.put(COL_NAME, name);
values.put(COL_ADDRESS, address);
values.put(COL_PHONE, phone);
values.put(COL_EMAIL, email);
db.insert(TABLE_NAME, null, values);
//db.execSQL("Insert into " +TABLE_NAME+ " VALUES('"+COL_ID+"','"+name+"','"+address+"','"+phone+"','"+email+"');");
db.close();
}
// now here I want to make a return type method to return "I don't know what data type or anything that will fit the listView
public void getData() {
DBHelper.getReadableDatabase();
}
}
我想创建一个返回适合ListView的方法。我应该返回arrayAdapter还是只返回一个简单的String数组?并且如果它是arrayAdapter我不知道我应该在其中放入什么类型(为了澄清我在这里的意思是这个ArrayAdapter“我究竟应该放在这里?将使用它的活动还是String?”)。该方法应该是什么样的?
我将非常感谢你的帮助。
答案 0 :(得分:0)
此解决方案适合我。您需要将public void getData()
方法声明为ArrayList或ArrayAdapter,如下所示:
public ArrayList<String> getData() {
ArrayList<String> values = new ArrayList<String>();
String columns[] = new String[] { COL_NAME }; //considering you wanna return name
Cursor c = db.query(TABLE_NAME, columns, null, null, null, null,
null);
String result;
int iName = c.getColumnIndex(COL_NAME);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = c.getString(iName);
values.add(result);
}
return values;
}
现在,在您想要显示列表视图的主类中,输入以下代码:
private DBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
dbHandler = new DBHandler(Classname.this);
dbHandler.open();
ArrayList<String> data = dbHandler.getData();
final ListView listView = getListView();
setListAdapter(new ArrayAdapter<String>(ExpSubList.this,
android.R.layout.simple_list_item_1, data));
}
这应该会在列表视图中显示名称。我没有使用xml布局来定义listview,直接在java中定义它。您可以根据需要进行操作。