[英]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.