接上
安卓SDK——Bmob云服务(登录注册)
https://blog.csdn.net/nishigesb123/article/details/89854563
这里完成一个便签案例
主要实现
- 添加便签
- 删除便签
- 修改便签内容
- 查询便签列表
新建一个实体类Note.class继承BmobObject (叫习惯了,话说这里应该叫“实体类”嘛?)
字段给一个context(内容)即可
package com.example.bombtest;
import cn.bmob.v3.BmobObject;
public class Note extends BmobObject {
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
再建NoteListActivity作为启动界面
布局选择用一个ListView来显示
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NoteListActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
>
</ListView>
</android.support.constraint.ConstraintLayout>
NoteListActivity
package com.example.bombtest;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;
public class NoteListActivity extends AppCompatActivity {
private ArrayList<Note> notes = new ArrayList<>();
private ListView listView;
NoteAdapter noteAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_list);
Bmob.initialize(this, "0bc8b0a9e9aea4bb31f4be7fe9e9ec38");
listView = findViewById(R.id.listView);
//单击事件,点击修改
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//列表项布局里的TextView
TextView textView_content = view.findViewById(R.id.note_item);
String content = textView_content.getText().toString();
String objectid = (String) view.getTag();
Intent intent = new Intent(NoteListActivity.this, NoteDetailActivity.class);
intent.putExtra("content", content);
intent.putExtra("objectid", objectid);
startActivity(intent);
}
});
//注册上下文菜单
registerForContextMenu(listView);
}
@Override
protected void onResume() {
super.onResume();
loadData();
}
//查询方法
private void loadData() {
BmobQuery<Note> query = new BmobQuery<>();
//限定查询条数,不写默认10条?
query.setLimit(50);
//findObject是一个异步的过程
query.findObjects(new FindListener<Note>() {
@Override
public void done(List<Note> list, BmobException e) {
if (e == null) {
notes = (ArrayList<Note>) list;
noteAdapter = new NoteAdapter(NoteListActivity.this, notes);
listView.setAdapter(noteAdapter);
}
}
});
}
static class NoteAdapter extends BaseAdapter {
private Context context;
private List<Note> list;
public NoteAdapter(Context context, List<Note> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.layout_note_item, null);
}
Note note = list.get(position);
TextView content = convertView.findViewById(R.id.note_item);
//为了显示
content.setText(note.getContent());
//存储ID
convertView.setTag(note.getObjectId());
return convertView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_note_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_note:
//点击添加,跳转到添加界面
Intent intent = new Intent(this, NoteNewActivity.class);
startActivity(intent);
break;
}
return true;
}
//创建上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//group id 自己的id 排序 文本内容
menu.add(1, 100, 100, "删除");
}
//点击时候长按删除
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 100:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
//targetView就是当前目标(当前点击的...)
View view = info.targetView;
String objectid = (String) view.getTag();
Note note = new Note();
note.setObjectId(objectid);
note.delete(new UpdateListener() {
@Override
public void done(BmobException e) {
if (e == null) {
loadData();
}
}
});
break;
}
return super.onContextItemSelected(item);
}
}
准备一个layout_note_item.xml作为列表项布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/note_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
准备一个NoteDetailActivity作为修改界面
布局如下,只需要一个编辑框即可
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NoteDetailActivity">
<EditText
android:id="@+id/edit_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
NoteDetailActivity
package com.example.bombtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.EditText;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
public class NoteDetailActivity extends AppCompatActivity {
private EditText edit_detail;
private String objectid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_detail);
edit_detail = findViewById(R.id.edit_detail);
objectid = getIntent().getStringExtra("objectid");
edit_detail.setText(getIntent().getStringExtra("content"));
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
String content = edit_detail.getText().toString();
if (!TextUtils.isEmpty(content)) {
//如果内容不为空点返回进行保存操作
Note note = new Note();
note.setContent(content);
note.update(objectid, new UpdateListener() {
@Override
public void done(BmobException e) {
}
});
}
}
return super.onKeyDown(keyCode, event);
}
}
准备一个menu_note_list菜单文件,来作为APP的菜单,作为添加功能的入口。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/add_note" android:title="添加"
android:orderInCategory="100" app:showAsAction="always"/>
</menu>
同时准备一个NoteNewActivity实现新建便签,布局给一个Edittext即可
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NoteNewActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/edit_new"/>
</android.support.constraint.ConstraintLayout>
NoteNewActivity
package com.example.bombtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.EditText;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
public class NoteNewActivity extends AppCompatActivity {
private EditText editText_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_new);
editText_content = findViewById(R.id.edit_new);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
System.out.println("ADD界面返回键被按下");
String content = editText_content.getText().toString();
if (!TextUtils.isEmpty(content)) {
Note note = new Note();
note.setContent(content);
note.save(new SaveListener<String>() {
@Override
public void done(String s, BmobException e) {
}
});
}
}
return super.onKeyDown(keyCode, event);
}
}
效果测试
整体界面
点击添加
再点击返回保存(字有点小,可以考虑设置的大一点)
点击ListItem进入修改界面
修改内容并返回
可以看到控制台的数据也发生了变化
长按会出现删除
刷新控制台