2011-12-07 21 views
1

送信されたデータはデータベースに保存されず、追加したくなかったコードはわかりません。アプリの流れは、送信されたと思われるデータをデータベースに保存することで、データベースはそのデータをタブのリストビューに表示します。Android:データをデータベースに挿入

btnSend.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       String phoneNo = editTextRecipient.getText().toString(); 
       String message = editTextNewMessage.getText().toString(); 
       Log.d(phoneNo, message); 
       saveState(phoneNo, message); 
       boolean split = false; 

       final Toast toast = Toast.makeText(getBaseContext(), 
         "Your message " + "\"" + message + "\"" + " is sent to " +"\""+ phoneNo+"\"", 
          Toast.LENGTH_SHORT); 

       Runnable showToastRunnable = new Runnable() { 
        public void run() { 
         dialog.cancel(); 
         toast.show(); 
        } 
       }; 

       if (phoneNo.length()>0 && message.length()>0) { 
        showProgress(); 
        if (count == 0) { 
          handler.postDelayed(showToastRunnable, 0); 
         } 
         else if (count == 1) { 
          handler.postDelayed(showToastRunnable, 15000); 
         } 
         else if (count == 2) { 
          handler.postDelayed(showToastRunnable, 30000); 
         } 
         else if (count == 3) { 
          handler.postDelayed(showToastRunnable, 60000); 
         } 
       } 

        // sendSMS(phoneNo, message, split); */ 
       else 
        Toast.makeText(getBaseContext(), 
         "Please enter both phone number and message.", 
         Toast.LENGTH_SHORT).show(); 
      } 
     });   
    } 

    private void populateFields() { 
     if (mRowId!=null) { 
      Cursor message=mDbHelper.fetchNote(mRowId); 
      startManagingCursor(message); 
      editTextRecipient.setText(message.getString(message.getColumnIndexOrThrow(MessagesDBAdapter.KEY_RECIPIENT))); 
      editTextNewMessage.setText(message.getString(message.getColumnIndexOrThrow(MessagesDBAdapter.KEY_MESSAGE))); 
     } 
    } 

    private void saveState(String phoneNo, String message) { 
     Log.i(phoneNo, message); 

     if (mRowId==null) { 
      mDbHelper.createNote(phoneNo, message); 
     } 
    } 

、ここでデータベースのコードです:ここでは、送信ボタンとデータベースへの移入フィールドのコードです

public class MessagesDBAdapter { 

    public static final String KEY_RECIPIENT = "recipient"; 
    public static final String KEY_MESSAGE = "body"; 
    public static final String KEY_ROWID = "_id"; 

    private static final String TAG = "NotesDbAdapter"; 
    private DatabaseHelper mDbHelper; 
    private SQLiteDatabase mDb; 


    //Database creation sql statement 

    private static final String DATABASE_CREATE = 
     "create table notes (_id integer primary key autoincrement, " 
     + "title text not null, body text not null);"; 

    private static final String DATABASE_NAME = "data"; 
    private static final String DATABASE_TABLE = "messages"; 
    private static final int DATABASE_VERSION = 2; 

    private final Context mCtx; 

    private static class DatabaseHelper extends SQLiteOpenHelper { 

     DatabaseHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 

      db.execSQL(DATABASE_CREATE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
        + newVersion + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS notes"); 
      onCreate(db); 
     } 
    } 

    /** 
    * Constructor - takes the context to allow the database to be 
    * opened/created 
    * 
    * @param ctx the Context within which to work 
    */ 
    public MessagesDBAdapter(Context ctx) { 
     this.mCtx = ctx; 
    } 

    public MessagesDBAdapter open() throws SQLException { 
     mDbHelper = new DatabaseHelper(mCtx); 
     mDb = mDbHelper.getWritableDatabase(); 
     return this; 
    } 

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

    public long createNote(String phoneNo, String message) { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_RECIPIENT, phoneNo); 
     initialValues.put(KEY_MESSAGE, message); 

     open(); 

     return mDb.insert(DATABASE_TABLE, null, initialValues); 
    } 

    public boolean deleteNote(long rowId) { 

     return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
    } 

    public Cursor fetchAllNotes() { 

     return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_RECIPIENT, 
       KEY_MESSAGE}, null, null, null, null, null); 
    } 

    public Cursor fetchNote(long rowId) throws SQLException { 

     Cursor mCursor = 

      mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, 
        KEY_RECIPIENT, KEY_MESSAGE}, KEY_ROWID + "=" + rowId, null, 
        null, null, null, null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 

    } 

    public boolean updateNote(long rowId, String phoneNo, String message) { 
     ContentValues args = new ContentValues(); 
     args.put(KEY_RECIPIENT, phoneNo); 
     args.put(KEY_MESSAGE, message); 

     return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0; 
    } 
} 
+0

//アプリケーションのデータベースのAndroidのデフォルトのシステムパスは。 \tパブリック文字列DB_PATH = "/data/data/com.minibible.cubix/databases/"; \t public static String DB_NAME = "MBC.db"; – SALMAN

+0

データベースへのパスが不足している可能性があります。あなたのコードにデータベースへのパスを記述していないからです。おかげで – SALMAN

+0

私は何をすべきですか?私は何を追加すべきですか? – Kev

答えて

0
public long insertData(String TableName, String[] ColumnNames, 
       String[] Values) { 

      String myPath = this.DB_PATH + DB_NAME; 

      this.dbHandler(); 

      ContentValues dataObj = new ContentValues(); 
      for (int i = 0; i < ColumnNames.length; i++) { 
       dataObj.put(ColumnNames[i], Values[i]); 
      } 
      return (this.db.insert(TableName, null, dataObj)); 

     } 



public void dbHandler() { 
     String myPath = this.DB_PATH + DB_NAME; 
     if (this.db != null) { 
      if (this.cursor != null) { 

       try { 
        // OPEN 
        this.cursor.close(); 
       } catch (Exception e) { 

       } 

      } 
      this.db.close(); 
      this.db = SQLiteDatabase.openDatabase(myPath, null, 
        SQLiteDatabase.OPEN_READWRITE); 
     } else { 
      this.db = SQLiteDatabase.openDatabase(myPath, null, 
        SQLiteDatabase.OPEN_READWRITE); 

     } 
    } 
関連する問題