20.11.2016 Views

ANDROID APP

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

238 บทที่ 9 การทำางานร่วมกับข้อมูล<br />

super.onCreate(savedInstanceState);<br />

setContentView(R.layout.diary);<br />

dba = new MyDB(this);<br />

dba.open();<br />

titleET = (EditText)findViewById(R.id.diarydescriptionText);<br />

contentET = (EditText)findViewById(R.id.diarycontentText);<br />

submitBT = (Button)findViewById(R.id.submitButton);<br />

submitBT.setOnClickListener(new OnClickListener() {<br />

public void onClick(View v) {<br />

try {<br />

saveItToDB();<br />

} catch (Exception e) {<br />

e.printStackTrace();<br />

}<br />

}<br />

});<br />

}<br />

public void saveItToDB() {<br />

dba.insertdiary(titleET.getText().toString(),<br />

contentET.getText().toString());<br />

dba.close();<br />

titleET.setText("");<br />

contentET.setText("");<br />

Intent i = new Intent(Diary.this, DisplayDiaries.class);<br />

startActivity(i);<br />

}<br />

คลาส DataStorage.java จะมีการทำงานเช่นเดียวกับ MyPreferences.class ซึ่งจะสั่งให้<br />

คลาส Diary.class เริ่มทำงานเมื่อล็อกอินสำเร็จ<br />

Toast.makeText(DataStorage.this, “login passed!!”,<br />

Toast.LENGTH_SHORT).show();<br />

Intent i = new Intent(DataStorage.this, Diary.class);<br />

startActivity(i);<br />

ในขั้นตอนสุดท้ายเป็นการแก้ไขไฟล์ Manifest เพื่อเพิ่มแอคทิวิตี้ใหม่เข้าไป ดังแสดงในชุด<br />

คำสั่งที่ 9.14<br />

ชุดคำสั่งที่ 9.14 AndroidManifest.xml<br />

<br />

<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!