列表视图内容删除查询使应用程序崩溃

时间:2019-06-20 23:50:59

标签: android sqlite android-database

这是一个账簿应用程序的代码,其总费用将显示在顶部,日期将显示在其下方。有一个查询试图删除条目,并在长按内容时将费用的总和减去已减去的总和。此处显示的应用图片:https://imgur.com/a/qUwqlFJ#X2etJ0S

但是,这会使应用程序崩溃,我不知道为什么,因为我在为Android开发人员做SQLite方面还很新。这是代码:

主要活动代码,其中怀疑发生了错误:

list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

                new AlertDialog.Builder(MainActivity.this).setIcon(android.R.drawable.ic_delete)
                .setTitle("Confirm Deletion")
                .setMessage("Are you sure to delete this content?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        EditText eContext = (EditText) findViewById(R.id.edit_context);
                        EditText ePrice = (EditText) findViewById(R.id.edit_price);
                        String contexts = eContext.getText().toString();
                        int price = Integer.parseInt(ePrice.getText().toString());
                        String today_Date = getToday_date();
                        Log.d(TAG, "Value confirmed" + contexts + ", " + price + ", " + today_Date);

                        //Deletion query
                        String query = "DELETE FROM " + TABLE_NAME + " WHERE " + KEY_CONTEXT + "=? AND "
                                + KEY_PRICE + "=? AND " + KEY_DATE + "=?";
                        db.execSQL(query, new String[]{contexts, ePrice.getText().toString(), String.format("%s", View_DATE)});

                        // Calculate sum value
                        String queryPriceSum = String.format(
                                " SELECT SUM(price) FROM %s WHERE date = '%s'", TABLE_NAME, View_DATE);
                        cursor = db.rawQuery(queryPriceSum, null);
                        cursor.moveToNext();
                        String sum = String.valueOf(cursor.getInt(0));
                        Log.d(TAG, "sum : " + sum);
                        sum_view.setText(sum);


                        // cursor.requery();
                        String querySelectAll = String.format("SELECT * FROM %s WHERE date = '%s'", TABLE_NAME, View_DATE);
                        cursor = db.rawQuery(querySelectAll, null);
                        myAdapter.changeCursor(cursor);
                        //myAdapter.notifyDataSetChanged();

                        eContext.setText("");
                        ePrice.setText(0);

                        //InputMethodManager imm =
                        //        (InputMethodManager) getSystemService( Context.INPUT_METHOD_SERVICE );
                        //imm.hideSoftInputFromWindow( ePrice.getWindowToken(), 0 );

                    }
                })
                .setNegativeButton("No", null)
                .show();
                return true;
            }
        });

DBHelper类:

public MyDBHelper(Context context) {

        super(context, "My_Account_Data.db", null, 4);
    }

    public void onCreate(SQLiteDatabase db) {
        String query = String.format("CREATE TABLE %s ("
                + "%s INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "%s TEXT, "
                + "%s INTEGER, "
                + "%s TEXT);", TABLE_NAME, KEY_ID, KEY_CONTEXT, KEY_PRICE, KEY_DATE);
        db.execSQL(query);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String query = String.format("DROP TABLE IF EXISTS %s", TABLE_NAME);
        db.execSQL(query);
        onCreate(db);
    }

游标适配器类:

 String TAG = "MyCursorAdapter";
    public MyCursorAdapter(Context context, Cursor c) {
        super(context, c);
    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from( context );
        View v = inflater.inflate( R.layout.list_item, parent,false );
        return v;

    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView item_context = (TextView) view.findViewById( R.id.item_context );
        TextView item_price = (TextView) view.findViewById( R.id.item_price );


        String contexts = cursor.getString( cursor.getColumnIndex( MainActivity.KEY_CONTEXT ) );
        String price = cursor.getString( cursor.getColumnIndex( MainActivity.KEY_PRICE ) );

        Log.d(TAG, contexts + ", " + price);

        item_context.setText( contexts );
        item_price.setText( price );

    }

Logcat:

2019-06-21 08:32:57.052 17280-17280/org.androidtown.mycalendar D/InputMethodManager: getNavigationBarColor() -855310
2019-06-21 08:32:57.188 17280-17280/org.androidtown.mycalendar D/ViewRootImpl@ef6be96[MainActivity]: ViewPostIme pointer 1
2019-06-21 08:32:57.190 17280-17280/org.androidtown.mycalendar D/AbsListView: onTouchUp() mTouchMode : -1
2019-06-21 08:32:57.650 17280-17280/org.androidtown.mycalendar D/ViewRootImpl@1632a00[MainActivity]: ViewPostIme pointer 0
2019-06-21 08:32:57.751 17280-17280/org.androidtown.mycalendar D/ViewRootImpl@1632a00[MainActivity]: ViewPostIme pointer 1
2019-06-21 08:32:57.768 17280-17280/org.androidtown.mycalendar D/AndroidRuntime: Shutting down VM
2019-06-21 08:32:57.770 17280-17280/org.androidtown.mycalendar E/AndroidRuntime: FATAL EXCEPTION: main
    Process: org.androidtown.mycalendar, PID: 17280
    java.lang.NumberFormatException: For input string: ""
        at java.lang.Integer.parseInt(Integer.java:627)
        at java.lang.Integer.parseInt(Integer.java:650)
        at org.androidtown.mycalendar.MainActivity$2$1.onClick(MainActivity.java:128)
        at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7045)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
2019-06-21 08:32:57.790 17280-17280/org.androidtown.mycalendar I/Process: Sending signal. PID: 17280 SIG: 9

对于查明问题和/或解决问题的任何帮助,我们将不胜感激,

1 个答案:

答案 0 :(得分:0)

似乎ePrice.getText().toString()为空,您是否在ePrices中设置了任何文本? 随时对此有任何疑问。