2015-04-12 69 views
-2

我正在關注sqllite教程數據庫的教程。錯誤未解決的類型,如何初始化dbhelper?

這裏是tutorial提供的代碼。

package com.example.addressbook; 

import java.util.ArrayList; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class MainActivity extends Activity { 
    public final static String EXTRA_MESSAGE = "com.example.AddressBook.MESSAGE"; 

    private ListView obj;  
    DBHelper mydb; 

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

     mydb = new DBHelper(this); 
     ArrayList array_list = mydb.getAllCotacts(); 

     ArrayAdapter arrayAdapter =  
     new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list); 

     //adding it to the list view. 
     obj = (ListView)findViewById(R.id.listView1); 
     obj.setAdapter(arrayAdapter); 

     obj.setOnItemClickListener(new OnItemClickListener(){ 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
    long arg3) { 
     // TODO Auto-generated method stub 
     int id_To_Search = arg2 + 1; 
     Bundle dataBundle = new Bundle(); 
     dataBundle.putInt("id", id_To_Search); 
     Intent intent = new Intent(getApplicationContext(),com.example.addressbook.DisplayContact.class); 
     intent.putExtras(dataBundle); 
     startActivity(intent); 
    } 
    }); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.mainmenu, menu); 
     return true; 
     } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     super.onOptionsItemSelected(item); 
     switch(item.getItemId()) 
     { 
     case R.id.item1: 
      Bundle dataBundle = new Bundle(); 
      dataBundle.putInt("id", 0); 
      Intent intent = new Intent(getApplicationContext(),com.example.addressbook.DisplayContact.class); 
      intent.putExtras(dataBundle); 
      startActivity(intent); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 

     } 

    } 
    public boolean onKeyDown(int keycode, KeyEvent event) { 
     if (keycode == KeyEvent.KEYCODE_BACK) { 
     moveTaskToBack(true); 
     } 
     return super.onKeyDown(keycode, event); 
    } 

} 

當我應用它,我有錯誤:

DBHelper mydb; 

DBHelper未解決的類型。 那麼如何聲明DBHelper?哪裏是我的代碼

+0

在教程中找到它,有'src/com.example.addressbook/DBHelper.java' – yummy

回答

1

本教程中的問題,你可以發現這一點:

Datbase - Helper class

For managing all the operations related to the datbase , an helper class has been given and is called SQLiteOpenHelper. It automatically manages the >creation and updation of the datbase. Its syntax is given below

public class DBHelper extends SQLiteOpenHelper { 
    public DBHelper(){ 
     super(context,DATABASE_NAME,null,1); 
    } 
    public void onCreate(SQLiteDatabase db) {} 
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {} 
} 
是實施例中,你可以找到實現類DBHelper

相關問題