Java 为什么我的主要活动有问题?
我希望我的应用程序能够在用户单击“获取平均值”按钮时显示平均值,但我如何才能做到这一点?我已经创建了数据库,数据库正在运行,但我对主要活动有问题 舱单:Java 为什么我的主要活动有问题?,java,android,android-sqlite,Java,Android,Android Sqlite,我希望我的应用程序能够在用户单击“获取平均值”按钮时显示平均值,但我如何才能做到这一点?我已经创建了数据库,数据库正在运行,但我对主要活动有问题 舱单: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp">
<ImageView
android:layout_width="150dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:src="@drawable/logo_jpg" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="Product:"
android:id="@+id/textView"
android:textStyle="bold"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:ems="10"/>
</TableRow>
<ListView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:id="@+id/averagelistView"
android:layout_alignParentLeft="true"
>
</ListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonaverage"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:onClick="Average"
android:text="Get Average"/>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="Get Average:"
android:onClick="average"
android:id="@+id/textView2"
android:textStyle="bold"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:ems="10"/>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
主要活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.averagerating2ndpage);
lvInfo = (ListView) findViewById(R.id.averagelistView);
txtProduct = (EditText) findViewById(R.id.editText);
txtAvg = (EditText) findViewById(R.id.editText2);
db = new Database_rbar(this);
dbAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, ArrayofName);
lvInfo.setAdapter(dbAdapter);
lvInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
currentId = results.get(position).getId();
txtProduct.setText(results.get(position).getProduct());
}
});
DisplayAll();
btnAvg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtAvg = db.getAverage();
}
});
}
public void DisplayAll() {
results = db.getAllResults();
ArrayofName.clear();
for (Result rs : results) {
ArrayofName.add(rs.getId() + ".\t" + rs.getProduct());
}
dbAdapter.notifyDataSetChanged();
txtProduct.setText("");
}
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.averagerating2ndpage);
lvInfo=(ListView)findViewById(R.id.averagelistView);
txtProduct=(EditText)findViewById(R.id.EditText);
txtAvg=(EditText)findViewById(R.id.editText2);
db=新数据库\u rbar(此);
dbAdapter=newarrayadapter(这是android.R.layout.simple_list_item_1,android.R.id.text1,ArrayofName);
lvInfo.setAdapter(dbAdapter);
lvInfo.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
currentId=results.get(position.getId();
setText(results.get(position.getProduct());
}
});
DisplayAll();
btnAvg.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
txtAvg=db.getAverage();
}
});
}
public void DisplayAll(){
results=db.getAllResults();
ArrayofName.clear();
对于(结果:结果){
add(rs.getId()+“\t”+rs.getProduct());
}
dbAdapter.notifyDataSetChanged();
txtProduct.setText(“”);
}
}
数据库:
public class Database_rbar extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = Environment.getExternalStorageDirectory().toString() + "/result.db";
private static final String TABLE_RESULT = "Result";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_RATING = "rating";
private static final String KEY_PHONE = "phone";
private static final String KEY_PRODUCT = "product";
private static final String KEY_EMAIL = "email";
public Database_rbar(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
//creating Tables
@Override
public void onCreate(SQLiteDatabase db){
String CREATE_RESULTS_TABLE = "CREATE TABLE " + TABLE_RESULT + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME +
" TEXT,"+ KEY_PHONE + " TEXT," + KEY_EMAIL + " TEXT," + KEY_PRODUCT + " TEXT," + KEY_RATING + " REAL" + ")";
db.execSQL(CREATE_RESULTS_TABLE);
}
//upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
//drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_RESULT);
//create tables again
onCreate(db);
}
public int getAverage(String product){
String countquery = "SELECT AVG(rating) * FROM" + TABLE_RESULT + "WHERE" + KEY_PRODUCT + "='" + product + "'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countquery, null);
cursor.close();
return cursor.getCount();
}
public List<Result> getAllResults() {
List<Result> resultList = new ArrayList<Result>();
String selectQuery = "SELECT * FROM " + TABLE_RESULT;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
//looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Result result = new Result();
result.setId(Integer.parseInt(cursor.getString(0)));
result.setName(cursor.getString(1));
result.setPhone(cursor.getString(2));
result.setEmail(cursor.getString(3));
result.setProduct(cursor.getString(4));
result.setRating(Float.parseFloat(cursor.getString(5)));
resultList.add(result);
} while (cursor.moveToNext());
}
return resultList;
}
}
公共类数据库\u rbar扩展了SQLiteOpenHelper{
私有静态最终int数据库_VERSION=2;
私有静态最终字符串数据库\u NAME=Environment.getExternalStorageDirectory().toString()+“/result.db”;
私有静态最终字符串表\u RESULT=“RESULT”;
私有静态最终字符串密钥\u ID=“ID”;
私有静态最终字符串键\u NAME=“NAME”;
私有静态最终字符串键\u RATING=“RATING”;
专用静态最终字符串键\u PHONE=“PHONE”;
私有静态最终字符串密钥\u PRODUCT=“PRODUCT”;
私有静态最终字符串密钥\u EMAIL=“EMAIL”;
公共数据库\u rbar(上下文){
super(上下文、数据库名称、null、数据库版本);
}
//创建表
@凌驾
public void onCreate(SQLiteDatabase db){
字符串CREATE_RESULTS_TABLE=“CREATE TABLE”+TABLE_RESULT+”(“+KEY_ID+”整型主键自动递增“+KEY_NAME+
“文本,“+KEY_手机+”文本,“+KEY_电子邮件+”文本,“+KEY_产品+”文本,“+KEY_评级+”真实“+”;
execSQL(创建结果表);
}
//升级数据库
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
//删除旧表(如果存在)
db.execSQL(“如果存在,则删除表格”+表格结果);
//再次创建表
onCreate(db);
}
公共整数getAverage(字符串乘积){
String countquery=“从“+表格结果+”中选择平均值(评级)*,其中“+键产品+”=“+产品+”;
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.rawQuery(countquery,null);
cursor.close();
返回cursor.getCount();
}
公共列表getAllResults(){
List resultList=new ArrayList();
String selectQuery=“SELECT*FROM”+表\u结果;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
结果=新结果();
setId(Integer.parseInt(cursor.getString(0));
result.setName(cursor.getString(1));
result.setPhone(cursor.getString(2));
result.setEmail(cursor.getString(3));
result.setProduct(cursor.getString(4));
result.setRating(Float.parseFloat(cursor.getString(5));
结果列表。添加(结果);
}while(cursor.moveToNext());
}
返回结果列表;
}
}
从您的代码txtAvg
是一个EditText
变量,但在onClick()
方法中,您从db.getAverage()
中指定了一个整数值
适当的方法是将整数转换为字符串,然后使用setText()
方法将其设置为txtAvg
:
txtAvg.setText(String.valueOf(db.getAverage()));
Database\u rbar
中的getAverage()
方法有一个String
参数,但在MainActivity
中调用它时,它没有参数:
txtAvg = db.getAverage();
它应该(从我可以从您的代码中推断)看起来更像:
txtAvg = db.getAverage("product_key_here");
可能还有其他错误,但这是堆栈跟踪引用的错误
编辑:斋月朱玛节已经正确地确定了另一个问题。此外,我希望您的countquery
字符串也会产生问题,因为它的空格数不正确
编辑2:在调用cursor.getCount()之前,还可以调用cursor.close()
。我希望这也会引发错误。您的问题是什么?我们需要更多信息sbtnavg.setOnClickListener(newview.OnClickListener(){@Override public void onClick(View v){txtAvg=db.getAverage();}});这是错误的部分它怎么了?错误:(62,28)错误:类数据库中的方法getAverage\u rbar canno