簡體   English   中英

為什么我的主要活動有問題?

[英]Why is the issue with my main activity?

我希望我的應用程序能夠在用戶點擊獲取平均按鈕時顯示平均評分,但我該怎么做? 我已經創建了數據庫並且數據庫正在運行,但是我的主要活動有問題。

顯現:

<?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("");
    }
}

數據庫:

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;
    }
}

從您的代碼txtAvg是一個EditText變量,但在onClick()方法中,您從db.getAverage()分配給一個整數值。

適當的方法是將整數轉換為字符串,然后使用setText()方法將其設置為txtAvg

txtAvg.setText(String.valueOf(db.getAverage()));

Database_rbar中的getAverage()方法有一個String參數,但是當您在MainActivity調用它時,它沒有參數:

txtAvg = db.getAverage();

它應該(從我可以從你的代碼中推斷出的)看起來更像是:

txtAvg = db.getAverage("product_key_here");

可能還有其他錯誤,但這是您的堆棧跟蹤所引用的錯誤。

編輯: Ramadan Juma 正確識別了另一個問題。 此外,我希望您的countquery String也會產生問題,因為它的空格數不正確。

編輯 2:您還可以在調用cursor.getCount()之前調用cursor.close() cursor.getCount() 我預計這也會引發錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM