2012-10-16 30 views
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); 
    } 


} 

,這是我的活動

StartMoving.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); 
    } 
} 

任何人都可以指出我做錯了什麼?

回答

0

請嘗試:(在你的DbHelper的接近方法)

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

您需要關閉您的可寫的數據庫,然後關閉助手。

UPDATE嘗試:

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

的updaet語句使用準備好的語句。

+0

很好的接近()的問題了,但它沒有更新 – philip

+1

請註明這個答案所接受和發佈,作爲另一個問題所以它也可以幫助其他人。謝謝 –

+0

@philip現在嘗試 – harshit

0

更改close方法是這樣的:

public void close(){ 
    ourDatabase.close(); 
} 
+0

不工作.... – philip

0

你改變方法不關閉SQLiteDatabase參考。拳關閉這個然後關閉DbHelper參考,像這樣:

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

良好的密切()的問題了,但它沒有更新 – philip