我希望我的应用程序能够在用户点击获取平均按钮时显示平均评分,但我该怎么办?我已经创建了数据库并且数据库正在运行,但我对主要活动有疑问。
清单:
<?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>
MainActivity:
@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("");
}
}
数据库:
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;
}
}
答案 0 :(得分:1)
您的代码PortletURLFactoryUtil.create(portletRequest, portletId, plid, lifecycle)
是txtAvg
变量,但在EditText
方法中,您可以从onClick()
分配一个整数值。
适当的方法是将整数转换为String,然后使用db.getAverage()
方法将其设置为txtAvg
:
setText()
答案 1 :(得分:0)
getAverage()
中的Database_rbar
方法有一个String
参数,但当您在MainActivity
中调用它时,它没有参数:
txtAvg = db.getAverage();
它应该(从我可以从你的代码中推断出来)看起来更像:
txtAvg = db.getAverage("product_key_here");
可能存在其他错误,但这是堆栈跟踪引用的错误。
编辑:Ramadan Juma正确地确定了另一个问题。此外,我希望您的countquery
String
也会产生问题,因为它的空格数不正确。
修改2 :在致电cursor.close()
之前,您也可以致电cursor.getCount()
。我希望这也会引发错误。