[安卓] 数据库为Android - P3改善应用程序
在 2 幸运的是,我们有 创建数据库 和 齐全的接口设计 应用. 在这一点上,我们不断完善景点
注意:
这个系列的剧本我会用英文写. 然而,这是我的英文写作风格“译自越南”那么你会很容易跟进.
这里: Android的工作室 1.2.2
Android的SDK 5.1.1
闵SDK: 4.0 (安卓 4.0 以上将被用于应用程序)
但是,你绝对可以做Eclipse和Android的下
步 1: 创建适配器项目
我们将通过RecyclerViewHolder创建项目适配器RecyclerView, 它会帮助我们的名单顺畅, 优化.
package cachhoc.net.tut.demodatabase;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ItemNoteAdapter extends RecyclerView.Adapter<ItemNoteAdapter.RecyclerViewHolder> {
// list of note
private List<Note> list = new ArrayList<Note>();
private Context context;
public ItemNoteAdapter(Context context, List<Note> list) {
this.context = context;
this.list = list;
}
@Override
public int getItemCount() {
return list.size();
}
/**
* connect to item view
*/
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View itemView = inflater.inflate(R.layout.item, viewGroup, false);
return new RecyclerViewHolder(itemView);
}
/**
* set data for item
*/
@Override
public void onBindViewHolder(RecyclerViewHolder viewHolder, int position) {
Note note = list.get(position);
viewHolder.tvTitle.setText(note.getTitle());
viewHolder.tvContent.setText(note.getContent());
}
public void addItem(int position, Note note) {
list.add(position, note);
notifyItemInserted(position);
}
public void removeItem(int position) {
list.remove(position);
notifyItemRemoved(position);
}
/**
* ViewHolder for item view of list
*/
public class RecyclerViewHolder extends RecyclerView.ViewHolder implements
OnClickListener {
public LinearLayout container;
public TextView tvTitle;
public TextView tvContent;
public RecyclerViewHolder(View itemView) {
super(itemView);
container = (LinearLayout) itemView.findViewById(R.id.item_container);
tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
tvContent = (TextView) itemView.findViewById(R.id.tv_content);
container.setOnClickListener(this);
}
// click item then display note
@Override
public void onClick(View v) {
MainActivity.showNote(context, list.get(getPosition()).getId());
}
}
}
以上我必须调用显示音符showNote的方法点击时 1 项目. 该方法是用类MainActivity.
步 2: 建设MainActivity
package cachhoc.net.tut.demodatabase;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ItemNoteAdapter adapter;
private List<Note> listNote = new ArrayList<>();
private Context context;
private DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
db = new DatabaseHelper(context);
connectView();
}
/**
* connect java with xml view
*/
private void connectView() {
// find Float Action Button
findViewById(R.id.fab).setOnClickListener(this);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_note);
// If the size of views will not change as the data changes.
recyclerView.setHasFixedSize(true);
// Setting the LayoutManager.
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
// Setting the adapter.
adapter = new ItemNoteAdapter(context, listNote);
recyclerView.setAdapter(adapter);
}
/**
* update list note when resume (open app or finish NoteActivity)
*/
public void onResume() {
super.onResume();
updateListNote();
}
/**
* select all note from database and set to ls
* use for loop to add into listNote.
* We must add all item in ls into listNote then adapter can update
* we add reverse ls to show new note at top of list
*/
private void updateListNote() {
// clear old list
listNote.clear();
// add all notes from database, reverse list
ArrayList<Note> ls = db.getListNote("SELECT * FROM " + DatabaseHelper.TABLE_NOTE);
// reverse list
for (int i = ls.size() - 1; i >= 0; i--) {
listNote.add(ls.get(i));
}
adapter.notifyDataSetChanged();
}
/**
* display note have id
*/
public static void showNote(Context context, long id) {
Intent intent = new Intent(context, NoteActivity.class);
// send id to NoteActivity
intent.putExtra(NoteActivity.ID, id);
context.startActivity(intent);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fab:
showNote(context, NoteActivity.NEW_NOTE);
break;
default:
break;
}
}
}
代码也是很容易理解. 只要 2 您可以共享下面的担忧:
- 为了能够解决它需要设置一个布局RecyclerView. 在他的代号为LinearLayoutManager同时RecyclerView名单将作为平滑的ListView. 如果使用,这将是一个网格GridLayoutManager.
-
仲TA选择的ArrayList
从分配给LS数据库. 然后,将这些元素复制到相当listNote 未分配的listNote = LS 因为那时listNote将采用不同的存储区域 (LS的存储器) 使得适配器不能被更新. 我们也反向LS分配给listNote,因为我希望把音符写在列表的顶部后,.
步 3: 创建注释起草NoteActivity
package cachhoc.net.tut.demodatabase;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
import android.support.v7.app.AlertDialog;
public class NoteActivity extends AppCompatActivity {
/**
* if action is add new note, it will init by NEW_NOTE
*/
public static final long NEW_NOTE = -1;
/**
* key for get id note from other activity to display note
*/
public static final String ID = "ID";
/**
* to edit, add,... note from database
*/
private DatabaseHelper db;
/**
* note curren
*/
private Note note;
private EditText editTitle;
private EditText editContent;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
context = this;
db = new DatabaseHelper(context);
connectView();
getInfo();
}
/**
* conect with xml view
*/
private void connectView() {
editTitle = (EditText) findViewById(R.id.edit_title);
editContent = (EditText) findViewById(R.id.edit_content);
}
/**
* get info note to display
*/
private void getInfo() {
long id = getIntent().getLongExtra(ID, NEW_NOTE);
// not new note then find note from database by id of note
if (id != NEW_NOTE) {
String sql = "SELECT * FROM " + DatabaseHelper.TABLE_NOTE + " WHERE " + DatabaseHelper.KEY_ID_NOTE + " = " + id;
note = db.getNote(sql);
}
if (note != null) {
editTitle.setText(note.getTitle());
editContent.setText(note.getContent());
} else {
editTitle.setText("");
editContent.setText("");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_note, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_save:
save();
break;
case R.id.menu_delete:
delete();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/**
* get title and content of note, if they empty then finish
* if they not empty then check note is null?
* if note is null (create new note), we will create note and insert into database
* if note not null then we update note
* after save we finish activity
*/
private void save() {
String title = editTitle.getText().toString().trim();
String content = editContent.getText().toString().trim();
String notify = null;
if (TextUtils.isEmpty(title) && TextUtils.isEmpty(content)) {
notify = "note empty, don't save!";
} else {
// new note
if (note == null) {
Note note = new Note();
note.setTitle(title).setContent(content);
if (db.insertNote(note) > 0) {
notify = "add success!";
} else {
notify = "add fail!";
}
} else { // update note
note.setTitle(title).setContent(content);
if (db.updateNote(note)) {
notify = "update success!";
} else {
notify = "update fail!";
}
}
}
Toast.makeText(context, notify, Toast.LENGTH_SHORT).show();
finish();
}
/**
* get title and content of note, if they empty then finish
* if they not empty then show dialog to show question delete
* if select no delete then close dialog
* if select delete then delete note
*/
private void delete() {
String title = editTitle.getText().toString().trim();
String content = editContent.getText().toString().trim();
if (TextUtils.isEmpty(title) && TextUtils.isEmpty(content)) {
finish();
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle(R.string.delete).setIcon(R.mipmap.ic_launcher)
.setMessage("Do you want delete note?");
builder.setPositiveButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
deleteNote();
}
});
builder.show();
}
}
/**
* check note is null?
* if is null then finish
* if note not null then delete in database and finish
*/
private void deleteNote() {
if (note != null) {
String where = DatabaseHelper.KEY_ID_NOTE + " = " + note.getId();
String notify = "delete success!";
if (!db.deleteNote(where)) {
notify = "delete failt!";
}
Toast.makeText(context, notify, Toast.LENGTH_SHORT).show();
}
finish();
}
/**
* click back button on phone
*/
@Override
public void onBackPressed() {
save();
}
}
在NoteActivity, 当它开始被发送到该ID的意图. NEU ID = NEW_NOTE (是 -1) 这意味着创建一个新的笔记, 我们的setText为editTitle和editContent空字符串. 否则,我们将注意从数据库ID并显示在.
代码完全乱了套. 我们希望你能完成申请.
您是当场就可以 下载项目DemoDatabase
部分 4 接下来,我将指导您如何更新数据库,如果不改变目前的应用程序错误.
在本教程的帖子 数据库的Android仲 由 nguyenvanquan7826



我按照说明在Android上创建数据库. 但是我得到这个错误:
10-22 09:55:29.233 12687-12687/? E / SQLiteLog: (1) 没有这样的表: tb_note
10-22 09:55:29.233 12687-12687/? D / AndroidRuntime: 关闭虚拟机
10-22 09:55:29.233 12687-12687/? W / dalvikvm: 线程ID = 1: 线程未捕获而退出 (组= 0xa617c908)
10-22 09:55:29.233 12687-12687/? E / AndroidRuntime: 致命异常: 主
10-22 09:55:29.233 12687-12687/? E / AndroidRuntime: 了java.lang.RuntimeException: 无法恢复活动 {nguyenduchai.cse.com.mynote / nguyenduchai.cse.com.mynote.MainActivity}: android.database.sqlite.SQLiteException: 没有这样的表: tb_note (代码 1): , 在编译时: 选择 * 来自tb_note
10-22 09:55:29.233 12687-12687/? E / AndroidRuntime: 在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
我一直都在修理,但我不能 ? 你告诉我如何解决…
你再看一次, 它说没有表tb_note. 请练习阅读错误.
我知道不创建表tb_note, 错误应被检索. 但是我已经仔细考虑了代码, 仔细观察您已采取的步骤. 但是还没有解决. 不知道为什么 ?
下午10:00左右,您可以发短信: fb.com/nguyenvanquan7826
行. 谢谢禁令.
我可以问你一个问题吗?: 我已按照帖子中的步骤进行操作. 我正在开会 1 问题在于添加新笔记后,每个笔记都会弹出 1 当然 1 屏幕页面, 需要拖动屏幕以查看其他注释, 不显示如视频所示. 所以, 我想这样显示然后?
您可以调整物品的高度. 做吧.
谢谢. 我已经解决了这个错误.