[英]Creating a working ListView activity with information retrieved from a SQLite Database
我正在做一個項目,要求我在第一個活動的列表視圖中顯示國家列表,當按下其中任何一個時,它將打開一個新活動,並相應顯示相應的數據。 我正在嘗試使用SQLite數據庫來實現。
我不確定我的代碼有什么問題,我已經在代碼上工作了幾個小時,但仍然無法找到問題所在。
這是我的MainActivity:
公共類MainActivity擴展了ListActivity {
private DatabaseHelper dbHelper;
private SimpleCursorAdapter dataAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new DatabaseHelper(this);
dbHelper.open();
//Generate ListView from SQLite Database
displayListView();
}
String [] countries = new String []{
DatabaseHelper.COUNTRY
};
private void displayListView() {
Cursor cursor = dbHelper.fetchAllCountries();
// The desired columns to be bound
String[] columns = new String[] {
DatabaseHelper.COUNTRY,
DatabaseHelper.CAPITAL,
DatabaseHelper.CURRENCY,
DatabaseHelper.POPULATION
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.text
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.country_info,
cursor,
countries,
to,
0);
ListView listView = (ListView) findViewById(R.id.label);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
這是我的DatabaseHelper:
private SQLiteDatabase mydb;
private static final String DATABASE_NAME="countries_db";
static final int DATABASE_VERSION = 1;
private static final String KEY_ID = "id";
static final String COUNTRY="country";
static final String CAPITAL="capital";
static final String CURRENCY="currency";
static final String POPULATION="population";
private static final String SQLITE_TABLE = "countries";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + SQLITE_TABLE + "(" + KEY_ID + "
INTEGER PRIMARY KEY," + COUNTRY + " TEXT," + CAPITAL + " TEXT" + CURRENCY
+ " TEXT," + POPULATION + " REAL" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
//this is populating the database
ContentValues cv=new ContentValues();
cv.put(COUNTRY, "EGYPT");
cv.put(CAPITAL, "CAIRO");
cv.put(CURRENCY, "EGP");
cv.put(POPULATION, "90");
db.insert("countries", null, cv);
cv.put(COUNTRY, "Jordan");
cv.put(CAPITAL, "Amman");
cv.put(CURRENCY, "Jordanian Dinar");
cv.put(POPULATION, "6.3");
db.insert("countries", null, cv);
cv.put(COUNTRY, "Kuwait");
cv.put(CAPITAL, "Kuwait City");
cv.put(CURRENCY, "Kuwait Dinar");
cv.put(POPULATION, "3.25");
db.insert("countries", null, cv);
cv.put(COUNTRY, "Saudi Arabia");
cv.put(CAPITAL, "Riyadh");
cv.put(CURRENCY, "Riyal");
cv.put(POPULATION, "29");
db.insert("countries", null, cv);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
onCreate(db);
}
public void open() {
// TODO Auto-generated method stub
}
public Cursor fetchAllCountries() {
SQLiteDatabase mydb = this.getWritableDatabase();
Cursor cursor = mydb.query(SQLITE_TABLE, new String[] {COUNTRY, CAPITAL, CURRENCY,
POPULATION},
null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
}
您的適配器正在查找數據庫查詢返回的名為_id的列。 您可以將主鍵更改為_id,也可以在數據庫上運行select查詢時執行
"SELECT 'selectColumns' 'yourPrimaryKey' as _id from 'yourTableName'"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.