我正在使用 sqlite 中的数据库,该数据库使用两个按钮和一个 EditText 字段来检索用户输入并在数据库中显示相应的记录。我的一个按钮出现错误,我不确定是什么原因造成的,我尝试实现各种代码行并进行清理,希望有人能对导致问题的原因有所了解.我得到的错误是我的按钮无法放入变量中。我已将 (*) 符号放在发生问题的位置。非常感谢任何帮助。
package com.example.database;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class DBUse extends Activity {
EditText dEdit;
SQLiteDatabase data;
public static final String KEY_ROWID = "_id";
public static final String KEY_BUSINESS = "business";
public static final String KEY_ADDRESS = "address";
public static final String KEY_PHONE = "phone";
public static final String KEY_HOURS = "hours";
public static final String KEY_WEB = "website";
public static final String KEY_TYPE = "type";
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainActivity datab = new MainActivity(this);
Button buttonName =(Button)findViewById(R.id.buttonName);
buttonName.setOnClickListener(startName);
**Button buttonType =(Button)findViewById(R.id.buttonType);
buttonType.setOnClickListener(startType);**
dEdit =(EditText)findViewById(R.id.textView1);
datab.open();
Cursor c = datab.getAllTitles();
if(c.moveToFirst())
{
do{DisplayTitle(c);
}while (c.moveToNext());
}
datab.open();
Cursor b = datab.getAllTitles();
if (b.moveToFirst())
DisplayTitle(b);
else
Toast.makeText(this,"No business found",Toast.LENGTH_LONG).show();
data.close();
data.close();
}
//displays the data
public void DisplayTitle(Cursor c) {
Toast.makeText(this,
"Name: " + c.getString(1)+"\n"+
"Address:" + c.getString(2)+"\n"+
"Phone:" + c.getString(3)+"\n"+
"Hours:" + c.getString(4)+"\n"+
"Website"+ c.getShort(5)+
"Type" + c.getShort(6),
Toast.LENGTH_LONG).show();
}
//Informs the user what is being searched
private OnClickListener startName=new OnClickListener(){
public void onClick (View v) {
Cursor cur=data.rawQuery("SELECT business,address,phone,hours,website,type FROM Business where name like %"+dEdit.getText().toString()+"%", null);
String result="";
int iRow=cur.getColumnIndex(KEY_ROWID);
int iName=cur.getColumnIndex(KEY_BUSINESS);
int iAddress=cur.getColumnIndex(KEY_ADDRESS);
int iPhone=cur.getColumnIndex(KEY_PHONE);
int iHours=cur.getColumnIndex(KEY_HOURS);
int iWebsite=cur.getColumnIndex(KEY_WEB);
int iType=cur.getColumnIndex(KEY_TYPE);
Toast.makeText(DBUse.this, "Searching by Name", Toast.LENGTH_LONG).show();
// log.d("result",DBUse.getString(0));
for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()){
result=result +cur.getString(iRow) + " "
+cur.getString(iName) + " "
+cur.getString(iAddress) + " "
+cur.getString(iPhone) + " "
+cur.getString(iHours) + " "
+cur.getString(iWebsite) + " "
+cur.getString(iType) + " " + "\n";
}
return;
};
//Informs the user what is being searched
**private OnClickListener startType=new OnClickListener(){
public void onClick (View v){
Cursor cur=data.rawQuery("SELECT business,address,phone,hours,website,type FROM Business where type like %"+dEdit.getText().toString()+"%", null);**
String result="";
int iRow=cur.getColumnIndex(KEY_ROWID);
int iName=cur.getColumnIndex(KEY_BUSINESS);
int iAddress=cur.getColumnIndex(KEY_ADDRESS);
int iPhone=cur.getColumnIndex(KEY_PHONE);
int iHours=cur.getColumnIndex(KEY_HOURS);
int iWebsite=cur.getColumnIndex(KEY_WEB);
int iType=cur.getColumnIndex(KEY_TYPE);
Toast.makeText(DBUse.this, "Searching by Name", Toast.LENGTH_LONG).show();
// log.d("result",DBUse.getString(0));
for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()){
result=result +cur.getString(iRow) + " "
+cur.getString(iName) + " "
+cur.getString(iAddress) + " "
+cur.getString(iPhone) + " "
+cur.getString(iHours) + " "
+cur.getString(iWebsite) + " "
+cur.getString(iType) + " " + "\n";
}
return;
}
};
};
}
问题错误信息:
Description Resource Path Location Type
startType cannot be resolved to a variable DBUse.java /database/src/com/example/database line 43 Java Problem