0

是的,我知道这个问题已经问了很多,但似乎我检查的所有问题都不能解决我的问题。所以我会直奔主题。

这是数据库类

SQLHandler.java

public class SQLHandler {
    public static final String KEY_ROOMMOVEHOLDER = "roommoveholder";
    public static final String KEY_ROOM = "room";
    public static final String KEY_ROOMWEIGHT = "roomweight";

    public static final String KEY_MOVENAME = "movename";
    public static final String KEY_ID1 = "_id";
    public static final String KEY_ID2 = "_id";

    public static final String KEY_MOVEDATE = "movedate";
    public static final String KEY_TOTALMOVEWEIGHT = "totalmoveweight";
    public static final String KEY_TOTALITEM = "totalitem";

    private static final String DATABASE_NAME = "mymovingfriend";
    private static final int DATABASE_VERSION = 1;

    public static final String KEY_TODOMOVE = "todomove";

    private static final String DATABASE_TABLE1 = "movingname";
    private static final String DATABASE_TABLE2 = "movingrooms";

    public static final String CREATE_TABLE_1 = "CREATE TABLE " + DATABASE_TABLE1 + " (" + 
            KEY_ID1 + " INTEGER PRIMARY KEY AUTOINCREMENT," + 
            KEY_MOVEDATE + " TEXT NOT NULL, " + 
            KEY_TOTALMOVEWEIGHT + " TEXT NOT NULL, " + 
            KEY_TOTALITEM + " INTEGER NOT NULL, " +
            KEY_MOVENAME + " TEXT NOT NULL);";

    public static final String CREATE_TABLE_2 = "CREATE TABLE " + DATABASE_TABLE2 + " (" + 
            KEY_ID2 + " INTEGER PRIMARY KEY AUTOINCREMENT," + 
            KEY_ROOMMOVEHOLDER + " TEXT NOT NULL, " + 
            KEY_ROOMWEIGHT + " TEXT NOT NULL, " + 
            KEY_ROOM + " TEXT NOT NULL);";


    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteOpenHelper{

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(CREATE_TABLE_1);
            db.execSQL(CREATE_TABLE_2);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE1);
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE2);
            onCreate(db);
        }
    }

    public SQLHandler(Context c){
        ourContext = c;
    }

    public SQLHandler open() throws SQLException{
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close(){
        ourHelper.close();
    }

    public void renameRoom(String movename, String roomname){
        ContentValues cv = new ContentValues();
        cv.put(KEY_ROOM, roomname);
        ourDatabase.update(DATABASE_TABLE2, cv, KEY_ROOMMOVEHOLDER + "='" + movename + "'" + " AND " + KEY_ROOM + "='" + roomname + "'", null);
    }


}

这是我的活动

开始移动.java

public class StartMoving extends Activity {

    Button returnBack, addmove, set, setCancel;
    SQLHandler startmoving;

    EditText newMove;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_moving);

        initializer();
        itemInitializer();

        Intent i = getIntent();     
        currentMove = i.getStringExtra("moveName");

        buttonLoad();
        currentMoveName.setText(currentMove);

        currentRoom = "None";
        roomName.setText(currentRoom);

        buttonSet();
        itemButton();
        checktodo();
        roomSpinner();      

        SharedPreferences tut_pref = PreferenceManager.getDefaultSharedPreferences(StartMoving.this);
        btut = tut_pref.getString("gettut", "null");

        if (btut.equals("yes")) {
            AlertDialog help1 = new AlertDialog.Builder(StartMoving.this).show();
            help1.setContentView(R.layout.add_room_tut);
            next = true;
            managehelp = true;
            tut_pref = PreferenceManager.getDefaultSharedPreferences(StartMoving.this);
            SharedPreferences.Editor tut = tut_pref.edit();
            tut.putString("gettut", "no");
            tut.commit();
        }else {

        }

    }

    private void buttonLoad() {
        try {

            ArrayList<String> sb = startmoving.loadRooms(currentMove);
            String[] rb = (String[]) sb.toArray(new String[sb.size()]);
            for (int i = 0; i < rb.length; i++) {
                final Button nb = new Button(StartMoving.this);
                nb.setText(rb[i]);
                row.addView(nb);
                nb.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        currentRoom = nb.getText().toString();
                        roomName.setText(currentRoom);

                        String displayname = startmoving.getItemName(currentMove, currentRoom);
                        String displayvalue = startmoving.getItemValue(currentMove, currentRoom);
                        String displayweight = startmoving.getItemWeightLBS(currentMove, currentRoom);
                        String displaytotalweight = startmoving.getTotalWeightLBS(currentMove);
                        String displayroomweight = startmoving.getRoomWeightLBS(currentMove, currentRoom);
                        int displaytotalitem = startmoving.getTotalItem(currentMove);
                        roomContent.setText(displayname);
                        itemValue.setText(displayvalue);
                        itemWeight.setText(displayweight);
                        totalweight.setText(displaytotalweight);
                        roomweight.setText(displayroomweight);
                        totalitem.setText("" + displaytotalitem);
                    }
                });
            }
            addRooms.addView(row);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void initializer() {

        startmoving = new SQLHandler(StartMoving.this);
        startmoving.open();

    }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main, menu);
        return true;
    }

    @SuppressLint("SdCardPath")
    @SuppressWarnings({ "deprecation" })
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        boolean retval = true;
        switch (item.getItemId()) {

        case R.id.menu_rename:
            final View view;
            LayoutInflater inf = LayoutInflater.from(StartMoving.this);
            view = inf.inflate(R.layout.rename, null);

            final EditText newname = (EditText) view.findViewById(R.id.etNewRoomName);

            new AlertDialog.Builder(StartMoving.this)
            .setView(view)
            .setTitle("Rename")
            .setMessage("Enter new name for room " + currentRoom)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    if (currentRoom.equals(newname.getText().toString())) {
                        Toast.makeText(StartMoving.this, "This name is already present. Please enter another name", Toast.LENGTH_LONG).show();
                    }else{
                        try {
                            String newroomname = newname.getText().toString();
                            startmoving.renameRoom(currentMove, newroomname);
//                          Intent intent = getIntent();
//                          finish();
//                          startActivity(intent);
                        } catch (Exception e) {
                            String error = e.toString();
                            Toast.makeText(StartMoving.this, error, Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            })
            .show();

            break;
        default:
            break;
        }
        return retval;
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        startmoving.close();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);

        roomName.setText("Hello world");
    }

    public void onBackPressed(){
        super.onBackPressed();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // do something on back.
            if (check == 1) {
                merge.setVisibility(View.GONE);
                main.setVisibility(View.VISIBLE);
                check = 0;
            }else if (check == 2) {
                main.setVisibility(View.VISIBLE);
                roomButtons.setVisibility(View.GONE);
                check = 0;
            }else {
                //              this.finish();
                Intent i = new Intent(StartMoving.this, ListMovingNames.class);
                startActivity(i);
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}

谁能指出我做错了什么?

4

3 回答 3

0

像这样更改您的关闭方法:

public void close(){
    ourDatabase.close();
}
于 2012-10-16T07:13:08.600 回答
0

您的更改方法没有关闭SQLiteDatabase参考。拳头关闭它,然后DbHelper像这样关闭您的参考:

public void close(){
    ourDatabase.close();
    ourHelper.close();
}
于 2012-10-16T07:21:37.447 回答
0

请尝试:(在您的 DbHelper 关闭方法中)

public void close(){
     ourDatabase.close();
     ourHelper.close();
}

您需要关闭可写数据库,然后关闭帮助程序。

更新尝试:

String[] whereClause = {movename, roomname};
ourDatabase.update(DATABASE_TABLE2, cv, KEY_ROOMMOVEHOLDER + "=? AND " + KEY_ROOM + "=?", whereClause);

updaet 语句使用准备好的语句。

于 2012-10-16T07:22:28.480 回答