0

I get db cannot be resloved error in this class. At the displayListView() function. Can anyone please help me solve it. Thanks

got code from here http://kdehairy.com/2012/08/19/using-a-preloaded-sqlite-database-with-sqliteopenhelper/

public class PrepopSqliteDbActivity extends Activity {

// private static final String DB_NAME = "hymnals";

//A good practice is to define database field names as constants



 private SimpleCursorAdapter dataAdapter;
 private Context context;

 @Override
  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Our key helper
    ExternalDbOpenHelper repo = ExternalDbOpenHelper.getInstance( context );

    SQLiteDatabase  db = repo.getWritableDatabase();


    displayListView();


}

 private void displayListView() {

 //all hymns are fetched 
  *final Cursor cursor = db.fetchAllHymns();*

  // The desired columns to be bound
  String[] columns = new String[] {

          ExternalDbOpenHelper.HYMN_ID,
          ExternalDbOpenHelper.HYMN_NAME,


  };

  // the XML defined views which the data will be bound to
  int[] to = new int[] {
    R.id.tVid,
    R.id.name,

  };

 // create the adapter using the cursor pointing to the desired data
 //as well as the layout information
     dataAdapter = new SimpleCursorAdapter(
    this, R.layout.activity_main,
    cursor,
    columns,
    to,
    0);

  ListView listView = (ListView) findViewById(R.id.list);
 // 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);

// Get the state's capital from this row in the database.

   }

  });


 }

  }        
4

1 回答 1

0

该变量dbonCreate方法内的局部变量。改用实例变量:

class /* ... */ {
    private SQLiteDatabase db;
    /* ... */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        /* ... */
        db = repo.getWritableDatabase();
        /* ... */
    }
    /* ... */
}
于 2013-08-16T00:11:46.790 回答