我编写了一个程序,从用户那里获取数据并将其存储在数据库中。这个数据库存储在资产文件夹中,当我编写程序并在行前使用断点时它存在
private OnClickListener mAddListener = new OnClickListener() {}
它给了我这个错误:找不到源。我该怎么办?
这是我的主要代码://------------主活动类---------
public class MainActivity extends Activity
{
BDAdapter db = new BDAdapter(this);
EditText name;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Capture our button from layout
Button setButton = (Button)findViewById(R.id.go);
Button getButton = (Button)findViewById(R.id.genRan);
// Register the onClick listener with the implementation above
setButton.setOnClickListener(mAddListener);
getButton.setOnClickListener(mAddListener);
}
// Create an anonymous implementation of OnClickListener
private OnClickListener mAddListener = new OnClickListener()
{
public void onClick(View v)
{
switch(v.getId())
{
case R.id.go:
db.open();
long id = 0;
// do something when the button is clicked
try
{
name = (EditText)findViewById(R.id.Quote);
db.insertQuote(name.getText().toString());
id = db.getAllEntries();
Context context = getApplicationContext();
CharSequence text = "The quote '" + name.getText() + "' was added successfully!\nQuotes Total = " + id;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
name.setText("");
}
catch (Exception ex)
{
Context context = getApplicationContext();
CharSequence text = ex.toString() + "ID = " + id;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
db.close();
break;
case R.id.genRan:
db.open();
//long id1 = 0;
// do something when the button is clicked
try
{
//String quote = "";
//quote = db.getRandomEntry();
//Context context = getApplicationContext();
//CharSequence text = quote;
//int duration = Toast.LENGTH_LONG;
//Toast toast = Toast.makeText(context, text, duration);
//toast.show();
db.open();
Cursor c = db.getAllTitles();
if (c.moveToFirst())
{
do {
DisplayTitle(c);
} while (c.moveToNext());
}
db.close();
}
catch (Exception ex)
{
Context context = getApplicationContext();
CharSequence text = ex.toString();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
db.close();
}
}
};
public void DisplayTitle(Cursor c)
{
Toast.makeText(this,
"NAME: " + c.getString(0) + "\n" ,
Toast.LENGTH_LONG).show();
}
}
//------------------------DBAdapter 类------------------------ ----
public class BDAdapter extends SQLiteOpenHelper {
private Context mycontext;
int id=0;
private String DB_PATH = "data/data/com.example.dd20/databases/";
private static String DB_NAME = "ff.sqlite";
public static final String KEY_ROWID = "_id";
public static final String KEY_QUOTE = "name";
public static final String DATABASE_TABLE = "ff1";
private static final String TAG = "BDAdapter";
public static final String DATABASE_CREATE =
"create table ff1 (_id integer primary key, "
+ "name text );";
// private DatabaseHelper DBHelper;
private SQLiteDatabase db;
// private SQLiteDatabase db;
// the extension may be .sqlite
// or .db
// public SQLiteDatabase myDataBase;
public BDAdapter(Context context) {
super(context, DB_NAME, null, 1);
this.mycontext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
// try {
// copyDataBase();
// } catch (IOException e) {
//
throw new Error("Error copying database");
// }
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
// private void copyDataBase() throws IOException{
//Open your local db as the input stream
// InputStream myInput = mycontext.getAssets().open(DB_NAME);
// Path to the just created empty db
// String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
// OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
// byte[] buffer = new byte[1024];
// int length;
// while ((length = myInput.read(buffer))>0){
// myOutput.write(buffer, 0, length);
// }
//Close the streams
// myOutput.flush();
// myOutput.close();
// myInput.close();
// }
public void open() {
// Open the database
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void close() {
if(db != null)
db.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
// + newVersion + ", which will destroy all old data");
// db.execSQL("DROP TABLE IF EXISTS ff1");
// onCreate(db);
}
public long insertQuote(String Quote)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_QUOTE, Quote);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public int getAllEntries()
{
Cursor cursor = db.rawQuery(
"SELECT COUNT(name) FROM ff1 ", null);
if(cursor.moveToFirst()) {
return cursor.getInt(0);
}
return cursor.getInt(0);
}
public String getRandomEntry()
{
id = getAllEntries();
Random random = new Random();
int rand = random.nextInt(getAllEntries());
if(rand == 0)
++rand;
Cursor cursor = db.rawQuery(
"SELECT name FROM ff1", null);
//"SELECT * FROM tblRandomQuotes",null);
if(cursor.moveToFirst()) {
return cursor.getString(0);
}
return cursor.getString(0);
}
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_QUOTE,
},
null,
null,
null,
null,
null);
}
}