Table of contents
I. Introduction
The first complete basic example project developed by Android beginners should be "Notepad". This project is developed based on Android Studio and uses Java language. The project includes the use of SQLlit database, listview, etc. Without further ado, here are the finished products:
Video effect display:
Simple notepad developed based on Android Studio
Picture effect display:
front page
Add page
Modify page
2. Detailed design
1.Home page
The user adds notes and uses listview to display the notes.
The code is as follows:
public class NotepadActivity extends Activity {
ListView listView;
List<NotepadBean> list;
SQLiteHelper mSQLiteHelper;
NotepadAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notepad);
//用于显示便签的列表
listView = (ListView) findViewById(R.id.listview);
ImageView add = (ImageView) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(NotepadActivity.this,
RecordActivity.class);
startActivityForResult(intent, 1);
}
});
initData();
}
}
private void showQueryData(){
if (list!=null){
list.clear();
}
//从数据库中查询数据(保存的标签)
list = mSQLiteHelper.query();
adapter = new NotepadAdapter(this, list);
listView.setAdapter(adapter);
}
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==1&&resultCode==2){
showQueryData();
}
}
}
2. Add and modify pages
Click the plus button on the home page or click the list item button to jump to the corresponding page.
Add the following code to the second page to determine the user click event.
protected void initData() {
mSQLiteHelper = new SQLiteHelper(this);
noteName.setText("添加记录");
Intent intent = getIntent();
if(intent!= null){
id = intent.getStringExtra("id");
if (id != null){
noteName.setText("修改记录");
content.setText(intent.getStringExtra("content"));
note_time.setText(intent.getStringExtra("time"));
note_time.setVisibility(View.VISIBLE);
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.note_back:
finish();
break;
case R.id.delete:
content.setText("");
break;
case R.id.note_save:
String noteContent=content.getText().toString().trim();
if (id != null){//修改操作
if (noteContent.length()>0){
if (mSQLiteHelper.updateData(id, noteContent, DBUtils.getTime())){
showToast("修改成功");
setResult(2);
finish();
}else {
showToast("修改失败");
}
}else {
showToast("修改内容不能为空!");
}
}else {
//向数据库中添加数据
if (noteContent.length()>0){
if (mSQLiteHelper.insertData(noteContent, DBUtils.getTime())){
showToast("保存成功");
setResult(2);
finish();
}else {
showToast("保存失败");
}
}else {
showToast("修改内容不能为空!");
}
}
break;
}