2013-10-13 105 views
1

我正在開發一個android應用程序,我想在textview中顯示主鍵,這樣每次我編輯一個文本字段時,我都會使用主鍵update.can任何人幫助我呢?下面是在sqlite中插入數據。我的問題是如何讓主鍵...插入數據庫後顯示主鍵

public class UsedataActivity extends Activity { 
    DatabaseHandler db = new DatabaseHandler(this); 
    ImageButton evsave; 
    EditText evname; 
    EditText evtime; 
    EditText evdate; 
    EditText evcode; 
    TextView evadmin; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_onetoone); 

     evsave = (ImageButton)findViewById(R.id.event_save); 
     evname = (EditText)findViewById(R.id.eventname); 
     evtime = (EditText)findViewById(R.id.time1); 
     evdate = (EditText)findViewById(R.id.eventdate); 
     evcode = (EditText)findViewById(R.id.eventcode); 
     evadmin = (TextView)findViewById(R.id.adminname_1to1); 

     evsave.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Events addev = 
        new Events(evname.getText().toString(),evcode.getText().toString(),evdate.getText().toString(),Integer.parseInt(evtime.getText().toString()),evadmin.getText().toString()); 

       db.addEvents(addev); 
       Toast.makeText(getApplicationContext(), "Event: "+ evname.getText()+" successfully save", 
           Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

數據庫處理類:

public void addEvents(Events event) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(KEY_EV_NAME, event.get_name()); 
    values.put(KEY_EV_PASS, event.get_pass()); 
    values.put(KEY_EV_DATE, event.get_date()); 
    values.put(KEY_EV_TIME, event.get_time()); 
    values.put(KEY_EV_ADMIN, event.get_admin()); 
    // Inserting Row 
    db.insert(TABLE_EVENTS, null, values); 
    db.close(); 
} 
+0

什麼是'DatabaseHandler'什麼'addEvents'方法呢? –

+0

數據庫處理器的是,我用它來創建表和列的addEvent類是在一個類中的方法,將獲得的值由用戶 –

+0

數據庫處理類 公共無效ADDEVENTS(活動事件){ \t \t SQLiteDatabase DB = inputed this.getWritableDatabase(); \t \t \t \t ContentValues values = new ContentValues(); \t \t values.put(KEY_EV_NAME,event.get_name()); \t \t values.put(KEY_EV_PASS,event.get_pass()); \t \t values.put(KEY_EV_DATE,event.get_date()); \t \t values.put(KEY_EV_TIME,event.get_time()); \t \t values.put(KEY_EV_ADMIN,event.get_admin()); \t \t \t // \t插入行 \t \t db.insert(TABLE_EVENTS,NULL,值); \t \t db.close(); \t} –

回答

1

因爲它可以從docs for the SQLiteDatabase可以觀察到,db.insert將返回新創建的id目的。只是讓addEvents返回它(而不是`無效)。

PS:請粘貼代碼編輯的問題,而不是評論。在評論中他們真的看起來很糟糕!

編輯

public long addEvents(Events event) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(KEY_EV_NAME, event.get_name()); 
    values.put(KEY_EV_PASS, event.get_pass()); 
    values.put(KEY_EV_DATE, event.get_date()); 
    values.put(KEY_EV_TIME, event.get_time()); 
    values.put(KEY_EV_ADMIN, event.get_admin()); 
    // Inserting Row 
    long id = db.insert(TABLE_EVENTS, null, values); 
    db.close(); 
    return id; 
} 

然後:

long id = db.addEvents(addev); 
Toast.makeText(getApplicationContext(), 
       "Event with id: "+ id + " successfully saved", 
       Toast.LENGTH_SHORT).show(); 
+0

我將如何做?im new to android你能幫助我嗎.. –

+0

@NicaSantos我加了相關的代碼 –

+0

謝謝boris它真的有效! :D –

相關問題