简体   繁体   中英

Creating a working ListView activity with information retrieved from a SQLite Database

I am working on a project which requires me to display a list of countries in a listview on the first activity, and when any of them are pressed, it opens a new activity with its corresponding data showing accordingly. I am trying to implement this using the SQLite database.

I am not sure what is wrong with my code, i have been working on it for hours and still can not seem to locate the problem.

This is my MainActivity:

public class MainActivity extends 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);

and this is my 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;
 }
}

Your adapter is looking for a column called _id returned by your database query. You can either change your primary key to _id or alternatively when you run your select query on your db do

"SELECT 'selectColumns' 'yourPrimaryKey' as _id from 'yourTableName'"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM