1.app功能:通过注册登录账户,拥有一个账户本,能够将平时自己容易的忘记的账户记录下来,并可以保持到云端,不需要担心数据丢失,只要登录账户,便可获取到自己的账户本。
2.实现的效果图,如下:
以下界面分别为注册界面、登录界面、提交账户内容界面、账户列表界面、长按删除账户信息、具体账户内容信息
3.实现的工程目录如下:
4.实现的具体过程:
a.布局代码:
(1)登录界面图:activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:background="#ffffff" 5 android:weightSum="5" 6 android:orientation="vertical" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 tools:context="base.secret.MainActivity"> 10 <LinearLayout 11 android:layout_weight="2" 12 android:layout_width="match_parent" 13 android:layout_height="0dp"/> 14 <base.secret.TextInputLayout 15 android:layout_marginRight="20dp" 16 android:layout_marginLeft="20dp" 17 android:gravity="center_vertical" 18 android:id="@+id/edit_user" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content"> 21 <EditText 22 android:textSize="20sp" 23 android:hint="@string/login_user" 24 android:gravity="center" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" /> 27 </base.secret.TextInputLayout> 28 <base.secret.TextInputLayout 29 android:layout_marginRight="20dp" 30 android:layout_marginLeft="20dp" 31 android:layout_marginTop="20dp" 32 android:id="@+id/edit_psw" 33 android:layout_width="match_parent" 34 android:layout_height="wrap_content"> 35 <EditText 36 android:inputType="textPassword" 37 android:textSize="20sp" 38 android:hint="@string/user_psw" 39 android:gravity="center" 40 android:layout_width="match_parent" 41 android:layout_height="wrap_content" /> 42 </base.secret.TextInputLayout> 43 <LinearLayout 44 android:layout_marginTop="20dp" 45 android:gravity="center" 46 android:orientation="horizontal" 47 android:layout_width="match_parent" 48 android:layout_height="wrap_content"> 49 <Button 50 android:textSize="25sp" 51 android:text="@string/login" 52 android:id="@+id/btn_login" 53 android:layout_width="wrap_content" 54 android:layout_height="wrap_content" /> 55 <Button 56 android:layout_marginLeft="20dp" 57 android:textSize="25sp" 58 android:text="@string/register" 59 android:id="@+id/btn_register" 60 android:layout_width="wrap_content" 61 android:layout_height="wrap_content" /> 62 </LinearLayout> 63 </LinearLayout>
(2)注册界面:register.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 android:weightSum="3" 4 xmlns:android="http://schemas.android.com/apk/res/android" 5 android:orientation="vertical" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent"> 8 <LinearLayout 9 android:layout_weight="1" 10 android:layout_width="wrap_content" 11 android:layout_height="0dp"/> 12 <base.secret.TextInputLayout 13 android:layout_marginTop="20dp" 14 android:layout_marginRight="20dp" 15 android:layout_marginLeft="20dp" 16 android:gravity="center_vertical" 17 android:id="@+id/register_user" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content"> 20 <EditText 21 android:textSize="20sp" 22 android:hint="@string/user_name" 23 android:gravity="center" 24 android:layout_width="match_parent" 25 android:layout_height="wrap_content" /> 26 </base.secret.TextInputLayout> 27 <base.secret.TextInputLayout 28 android:layout_marginRight="20dp" 29 android:layout_marginLeft="20dp" 30 android:layout_marginTop="20dp" 31 android:id="@+id/register_psw" 32 android:layout_width="match_parent" 33 android:layout_height="wrap_content"> 34 <EditText 35 android:textSize="20sp" 36 android:inputType="textPassword" 37 android:hint="@string/user_psw" 38 android:gravity="center" 39 android:layout_width="match_parent" 40 android:layout_height="wrap_content" /> 41 </base.secret.TextInputLayout> 42 <base.secret.TextInputLayout 43 android:layout_marginRight="20dp" 44 android:layout_marginLeft="20dp" 45 android:layout_marginTop="20dp" 46 android:id="@+id/register_again_psw" 47 android:layout_width="match_parent" 48 android:layout_height="wrap_content"> 49 <EditText 50 android:textSize="20sp" 51 android:hint="@string/again_psw" 52 android:inputType="textPassword" 53 android:gravity="center" 54 android:layout_width="match_parent" 55 android:layout_height="wrap_content" /> 56 </base.secret.TextInputLayout> 57 <Button 58 android:layout_marginTop="20dp" 59 android:textSize="25sp" 60 android:layout_gravity="center_horizontal" 61 android:text="@string/register" 62 android:id="@+id/btn_register2" 63 android:layout_width="wrap_content" 64 android:layout_height="wrap_content" /> 65 </LinearLayout>
(3)列表界面:activity_list.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent"> 7 <Button 8 android:layout_gravity="center" 9 android:layout_marginTop="20dp" 10 android:textSize="20sp" 11 android:text="@string/btn_add" 12 android:id="@+id/btn_add" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" /> 15 <ListView 16 android:layout_marginTop="10dp" 17 android:id="@+id/list" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content"> 20 21 </ListView> 22 23 </LinearLayout>
(4)列表中加载的布局:item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent"> 7 <TextView 8 android:id="@+id/text_item" 9 android:layout_marginTop="20dp" 10 android:layout_marginLeft="20dp" 11 android:textSize="25sp" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" /> 14 </LinearLayout>
(5)添加账户页面:activity_add.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:weightSum="3" 5 android:orientation="vertical" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent"> 8 <LinearLayout 9 android:layout_weight="1" 10 android:layout_width="wrap_content" 11 android:layout_height="0dp"/> 12 <base.secret.TextInputLayout 13 android:layout_marginTop="20dp" 14 android:layout_marginRight="20dp" 15 android:layout_marginLeft="20dp" 16 android:gravity="center_vertical" 17 android:id="@+id/commit_content" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content"> 20 <EditText 21 android:textSize="20sp" 22 android:hint="@string/more_message" 23 android:gravity="center" 24 android:layout_width="match_parent" 25 android:layout_height="wrap_content" /> 26 </base.secret.TextInputLayout> 27 <base.secret.TextInputLayout 28 android:layout_marginTop="20dp" 29 android:layout_marginRight="20dp" 30 android:layout_marginLeft="20dp" 31 android:gravity="center_vertical" 32 android:id="@+id/commit_user" 33 android:layout_width="match_parent" 34 android:layout_height="wrap_content"> 35 <EditText 36 android:textSize="20sp" 37 android:hint="@string/login_user" 38 android:gravity="center" 39 android:layout_width="match_parent" 40 android:layout_height="wrap_content" /> 41 </base.secret.TextInputLayout> 42 <base.secret.TextInputLayout 43 android:layout_marginRight="20dp" 44 android:layout_marginLeft="20dp" 45 android:layout_marginTop="20dp" 46 android:id="@+id/commit_psw" 47 android:layout_width="match_parent" 48 android:layout_height="wrap_content"> 49 <EditText 50 android:textSize="20sp" 51 android:hint="@string/user_psw" 52 android:gravity="center" 53 android:layout_width="match_parent" 54 android:layout_height="wrap_content" /> 55 </base.secret.TextInputLayout> 56 <Button 57 android:layout_marginTop="20dp" 58 android:textSize="25sp" 59 android:layout_gravity="center_horizontal" 60 android:text="@string/commit" 61 android:id="@+id/btn_commit" 62 android:layout_width="wrap_content" 63 android:layout_height="wrap_content" /> 64 </LinearLayout>
(6)账户具体信息页面:activity_detail.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:weightSum="2" 6 android:paddingLeft="20dp" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent"> 9 <LinearLayout 10 android:layout_weight="1" 11 android:layout_width="match_parent" 12 android:layout_height="0dp"/> 13 <TextView 14 android:text="@string/user" 15 android:textSize="18sp" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" /> 18 <TextView 19 android:layout_marginTop="10dp" 20 android:id="@+id/text_username" 21 android:textSize="25sp" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" /> 24 <TextView 25 android:layout_marginTop="20dp" 26 android:text="@string/psw" 27 android:textSize="18sp" 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" /> 30 <TextView 31 android:layout_marginTop="10dp" 32 android:textSize="30sp" 33 android:id="@+id/text_psw" 34 android:layout_width="match_parent" 35 android:layout_height="wrap_content" /> 36 </LinearLayout>
b.实现代码:
(1)主界面:MainActivity——主要是实现跳转页面的功能和利用BmobUser对象判断用户名登录是否正确:
1 package base.secret; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.support.annotation.MainThread; 7 import android.support.design.widget.*; 8 import android.support.design.widget.TextInputLayout; 9 import android.view.View; 10 import android.view.Window; 11 import android.widget.Toast; 12 13 import cn.bmob.v3.Bmob; 14 import cn.bmob.v3.BmobQuery; 15 import cn.bmob.v3.BmobUser; 16 import cn.bmob.v3.exception.BmobException; 17 import cn.bmob.v3.listener.SaveListener; 18 19 public class MainActivity extends Activity implements View.OnClickListener { 20 private android.support.design.widget.TextInputLayout editName,editPsw; 21 //此处定义自己app的ID 22 private String ID=""; 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 this.requestWindowFeature(Window.FEATURE_NO_TITLE); 27 setContentView(R.layout.activity_main); 28 Bmob.initialize(MainActivity.this,ID); 29 editName=(TextInputLayout)findViewById(R.id.edit_user); 30 editPsw=(TextInputLayout)findViewById(R.id.edit_psw); 31 findViewById(R.id.btn_login).setOnClickListener(this); 32 findViewById(R.id.btn_register).setOnClickListener(this); 33 } 34 35 @Override 36 public void onClick(View v) { 37 switch (v.getId()){ 38 case R.id.btn_login: 39 editName.setError(""); 40 editPsw.setError(""); 41 Login(); 42 break; 43 case R.id.btn_register: 44 Intent intentRegister=new Intent(MainActivity.this,RegisterActivity.class); 45 startActivity(intentRegister); 46 break; 47 } 48 } 49 private void Login(){ 50 String name=editName.getEditText().getText().toString(); 51 String psw=editPsw.getEditText().getText().toString(); 52 if (name.equals("")){ 53 editName.setError("用户名不能为空"); 54 editName.requestFocus(); 55 return ; 56 }else if (psw.equals("")){ 57 editPsw.setError("密码不能为空"); 58 editPsw.requestFocus(); 59 return ; 60 } 61 //获取一个BmobUser对象,调用login函数,如果登录成功,跳转到列表界面,否则提示错误 62 BmobUser user=new BmobUser(); 63 user.setUsername(name); 64 user.setPassword(psw); 65 user.login(new SaveListener<BmobUser>() { 66 @Override 67 public void done(BmobUser bmobUser, BmobException e) { 68 if (e==null){ 69 Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show(); 70 Intent i=new Intent(MainActivity.this,ListActivity.class); 71 startActivity(i); 72 }else{ 73 Toast.makeText(MainActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show(); 74 } 75 } 76 }); 77 } 78 }
(2)注册界面:RegisterActivity——主要实现用户名注册,获取到用户输入的数据,并上传到云端
1 package base.secret; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.support.design.widget.TextInputLayout; 7 import android.view.View; 8 import android.view.Window; 9 import android.widget.Toast; 10 11 import cn.bmob.v3.Bmob; 12 import cn.bmob.v3.BmobUser; 13 import cn.bmob.v3.exception.BmobException; 14 import cn.bmob.v3.listener.SaveListener; 15 16 17 public class RegisterActivity extends Activity implements View.OnClickListener { 18 private String ID=""; 19 private android.support.design.widget.TextInputLayout editName,editPsw,editAgainPsw; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 this.requestWindowFeature(Window.FEATURE_NO_TITLE); 24 setContentView(R.layout.activity_register); 25 Bmob.initialize(RegisterActivity.this,ID); 26 editName=(TextInputLayout)findViewById(R.id.register_user); 27 editPsw=(TextInputLayout)findViewById(R.id.register_psw); 28 editAgainPsw=(TextInputLayout)findViewById(R.id.register_again_psw); 29 findViewById(R.id.btn_register2).setOnClickListener(this); 30 } 31 32 @Override 33 public void onClick(View v) { 34 String name=editName.getEditText().getText().toString(); 35 String psw=editPsw.getEditText().getText().toString(); 36 String psw_again=editAgainPsw.getEditText().getText().toString(); 37 editName.setError(""); 38 editPsw.setError(""); 39 editAgainPsw.setError(""); 40 if (isValid(name,psw,psw_again)) storeUserMessage(name,psw); 41 } 42 public void storeUserMessage(String name,String psw){ 43 //保存用户信息到云端 44 BmobUser user=new BmobUser(); 45 user.setUsername(name); 46 user.setPassword(psw); 47 user.signUp(new SaveListener<BmobUser>() { 48 @Override 49 public void done(BmobUser bmobUser, BmobException e) { 50 if (e==null){ 51 Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show(); 52 Intent i=new Intent(RegisterActivity.this,MainActivity.class); 53 startActivity(i); 54 }else{ 55 Toast.makeText(RegisterActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show(); 56 } 57 } 58 }); 59 } 60 61 public boolean isValid(String name,String psw1,String psw2){ 62 if (name.equals("")){ 63 editName.setError("用户名不能为空"); 64 return false; 65 //判断用户名是否为数字和字母 66 }else if (!name.matches("^[a-zA-Z0-9]+$")){ 67 editName.setError("用户名只能包含字母和数字"); 68 return false; 69 } 70 else if (psw1.equals("")){ 71 editPsw.setError("密码不能为空"); 72 return false; 73 }else if (psw2.equals("")){ 74 editAgainPsw.setError("密码不能为空"); 75 return false; 76 } 77 if (psw1.equals(psw2)) return true; 78 else{ 79 editAgainPsw.setError("密码不一致"); 80 return false; 81 } 82 } 83 84 85 86 }
(3)建立一个UserMessage类来保存数据,主要含有用户名,密码和相关信息,因为要将数据上传到云端,所以该类要继承bmob的一个BmobObject对象。
1 package base.secret; 2 import cn.bmob.v3.BmobObject; 3 public class UserMessage extends BmobObject { 4 private String user; 5 private String psw; 6 private String message; 7 public UserMessage(){ 8 } 9 public UserMessage(String user, String psw, String message) { 10 this.user = user; 11 this.psw = psw; 12 this.message = message; 13 } 14 public UserMessage(String tableName, String user, String psw, String message) { 15 this.user = user; 16 this.psw = psw; 17 this.message = message; 18 this.setTableName(tableName); 19 } 20 public String getUser() { 21 return user; 22 } 23 public void setUser(String user) { 24 this.user = user; 25 } 26 public String getPsw() { 27 return psw; 28 } 29 public void setPsw(String psw) { 30 this.psw = psw; 31 } 32 public String getMessage() { 33 return message; 34 } 35 public void setMessage(String message) { 36 this.message = message; 37 } 38 }
(4)建立完后,我们要自定义一个适配器UserAdapter去加载数据到列表界面中:
1 package base.secret; 2 3 import android.content.Context; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.ArrayAdapter; 8 import android.widget.TextView; 9 import java.util.List; 10 public class UserAdapter extends ArrayAdapter<UserMessage>{ 11 private int resourceId; 12 public UserAdapter(Context context, int resource, List<UserMessage> objects) { 13 super(context, resource, objects); 14 resourceId=resource; 15 } 16 @Override 17 public View getView(int position, View convertView, ViewGroup parent) { 18 UserMessage message=getItem(position); 19 View view; 20 //将布局文件解析为view的方法 21 view= LayoutInflater.from(getContext()).inflate(resourceId,null); 22 TextView textView=(TextView)view.findViewById(R.id.text_item); 23 textView.setText(message.getMessage()); 24 return view; 25 } 26 }
(5)添加账户界面:AddActivity——主要实现获取到用户输入的信息,然后保存到云端中,这里涉及到一个bmob的使用问题,按理我们应该为每个用户都创建一个表来存储用户的信息,为了更好地从bmob云端获取到数据,我这里设置用户名作为表名,而且设置了用户名为唯一键,即不可重复。
1 package base.secret; 2 import android.app.Activity; 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.design.widget.*; 6 import android.support.design.widget.TextInputLayout; 7 import android.util.Log; 8 import android.view.View; 9 import android.view.Window; 10 import android.widget.Toast; 11 12 import java.util.List; 13 14 import cn.bmob.v3.Bmob; 15 import cn.bmob.v3.BmobObject; 16 import cn.bmob.v3.BmobUser; 17 import cn.bmob.v3.exception.BmobException; 18 import cn.bmob.v3.listener.SaveListener; 19 import cn.bmob.v3.listener.UpdateListener; 20 public class AddActivity extends Activity implements View.OnClickListener { 21 private String ID=""; 22 private android.support.design.widget.TextInputLayout editMessage,editUsername,editPassword; 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 requestWindowFeature(Window.FEATURE_NO_TITLE); 27 setContentView(R.layout.activity_add); 28 Bmob.initialize(AddActivity.this,ID); 29 editMessage=(android.support.design.widget.TextInputLayout)findViewById(R.id.commit_content); 30 editUsername=(TextInputLayout)findViewById(R.id.commit_user); 31 editPassword=(TextInputLayout)findViewById(R.id.commit_psw); 32 findViewById(R.id.btn_commit).setOnClickListener(this); 33 } 34 @Override 35 public void onClick(View v) { 36 String content=editMessage.getEditText().getText().toString(); 37 String user=editUsername.getEditText().getText().toString(); 38 String psw=editPassword.getEditText().getText().toString(); 39 if (content.equals("")){ 40 editMessage.setError("内容不能为空"); 41 editMessage.requestFocus(); 42 }else if (user.equals("")){ 43 editUsername.setError("用户名不能为空"); 44 editUsername.requestFocus(); 45 }else if (psw.equals("")){ 46 editPassword.setError("密码不能为空"); 47 editPassword.requestFocus(); 48 }else{ 49 //获取当前用户的用户名,等同于bmob云端上的表名 50 BmobUser bmobUser=BmobUser.getCurrentUser(); 51 String str=bmobUser.getUsername(); 52 //将数据传入到表格中,然后保存到云端 53 UserMessage userMessage=new UserMessage(str,user,psw,content); 54 userMessage.save(new SaveListener<String>() { 55 @Override 56 public void done(String s, BmobException e) { 57 if (e==null){ 58 Toast.makeText(AddActivity.this,"保存成功",Toast.LENGTH_SHORT).show(); 59 Intent i=new Intent(AddActivity.this, ListActivity.class); 60 startActivity(i); 61 finish(); 62 }else{ 63 Log.d("tag","保存失败"+e.getMessage()); 64 Toast.makeText(AddActivity.this,"保存失败:"+e.getMessage(),Toast.LENGTH_SHORT).show(); 65 } 66 } 67 }); 68 } 69 } 70 }
(6)添加完信息后,我们应该将在列表界面中更新出相关数据出来,那么我们去更新数据,即从bmob云端中获取数据,那么这里要去搜索与用户相对应的信息,而这里我们直接去搜索表名为用户名的表格即可,因为我们之前已经设置了表名为用户名,但这里要注意的是,从bmob云端中获取到的数据是json类型数据,所以我们还要对其进行解析。以及长按删除列表项的功能和点击跳转到详情页面。
1 package base.secret; 2 3 import android.app.Activity; 4 import android.content.DialogInterface; 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.support.v7.app.AlertDialog; 8 import android.util.Log; 9 import android.view.View; 10 import android.view.Window; 11 import android.widget.AdapterView; 12 import android.widget.ListView; 13 import android.widget.Toast; 14 15 import org.json.JSONArray; 16 import org.json.JSONException; 17 import org.json.JSONObject; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 22 import cn.bmob.v3.Bmob; 23 import cn.bmob.v3.BmobQuery; 24 import cn.bmob.v3.BmobUser; 25 import cn.bmob.v3.exception.BmobException; 26 import cn.bmob.v3.listener.FindListener; 27 import cn.bmob.v3.listener.QueryListener; 28 import cn.bmob.v3.listener.UpdateListener; 29 public class ListActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener { 30 private List<UserMessage> listContent=new ArrayList<>(); 31 private ListView listView; 32 private String ID=""; 33 private UserAdapter adapter; 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 requestWindowFeature(Window.FEATURE_NO_TITLE); 38 setContentView(R.layout.activity_list); 39 Bmob.initialize(ListActivity.this,ID); 40 listView=(ListView)findViewById(R.id.list); 41 //绑定适配器 42 adapter=new UserAdapter(ListActivity.this,R.layout.item,listContent); 43 //更新数据 44 getData(); 45 listView.setOnItemClickListener(this); 46 findViewById(R.id.btn_add).setOnClickListener(this); 47 listView.setOnItemLongClickListener(this); 48 } 49 public void getData() { 50 //获取到当前用户名 51 BmobUser user=BmobUser.getCurrentUser(); 52 //搜索bmob中表名与用户名等同的表 53 BmobQuery query=new BmobQuery(user.getUsername()); 54 query.findObjectsByTable(new QueryListener<JSONArray>() { 55 @Override 56 public void done(JSONArray jsonArray, BmobException e) { 57 if (e==null){ 58 for (int i=0;i<jsonArray.length();i++){ 59 try { 60 //先将json数组解析为json对象 61 JSONObject jsonObject=jsonArray.getJSONObject(i); 62 //然后再分别获取到相关对应的数据 63 String userName=jsonObject.getString("user"); 64 String password=jsonObject.getString("psw"); 65 String message=jsonObject.getString("message"); 66 String ObjectId=jsonObject.getString("objectId"); 67 //将获取的数据保存到表中 68 UserMessage userMessage=new UserMessage(userName,password,message); 69 userMessage.setObjectId(ObjectId); 70 //然后再添加到列表中 71 listContent.add(userMessage); 72 } catch (JSONException e1) { 73 e1.printStackTrace(); 74 } 75 } 76 //更新适配器的内容 77 listView.setAdapter(adapter); 78 adapter.notifyDataSetChanged(); 79 Toast.makeText(ListActivity.this,"更新成功"+jsonArray.length(),Toast.LENGTH_SHORT).show(); 80 }else { 81 Toast.makeText(ListActivity.this,"更新失败"+e.getMessage(),Toast.LENGTH_SHORT).show(); 82 } 83 } 84 }); 85 } 86 @Override 87 public void onClick(View v) { 88 switch (v.getId()){ 89 case R.id.btn_add: 90 Intent i=new Intent(ListActivity.this,AddActivity.class); 91 startActivityForResult(i,1); 92 finish(); 93 break; 94 } 95 } 96 @Override 97 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 98 //获取到当前点击的内容信息 99 UserMessage userMessage=listContent.get(position); 100 String Id=userMessage.getObjectId(); 101 //跳转并传递数据到账户详细信息界面 102 Intent intent=new Intent(ListActivity.this,DetailActivity.class); 103 intent.putExtra("name",userMessage.getUser()); 104 intent.putExtra("password",userMessage.getPsw()); 105 startActivity(intent); 106 } 107 108 @Override 109 public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { 110 //实现长按删除功能 111 AlertDialog.Builder builder=new AlertDialog.Builder(ListActivity.this); 112 builder.setMessage("是否要删除该内容?").setNegativeButton("取消", new DialogInterface.OnClickListener() { 113 @Override 114 public void onClick(DialogInterface dialog, int which) { 115 116 } 117 }).setPositiveButton("确定", new DialogInterface.OnClickListener() { 118 @Override 119 public void onClick(DialogInterface dialog, int which) { 120 //获取到要删除的表 121 BmobUser bmobUser=BmobUser.getCurrentUser(); 122 UserMessage user=new UserMessage(); 123 user.setTableName(bmobUser.getUsername()); 124 //删除在云端的信息 125 user.delete(listContent.get(position).getObjectId(), new UpdateListener() { 126 @Override 127 public void done(BmobException e) { 128 if (e==null){ 129 Toast.makeText(ListActivity.this,"删除成功",Toast.LENGTH_SHORT).show(); 130 //删除列表中的信息,并更新 131 listContent.remove(position); 132 adapter.notifyDataSetChanged(); 133 } 134 else{ 135 Toast.makeText(ListActivity.this,"删除失败"+e.getMessage(),Toast.LENGTH_SHORT).show(); 136 Log.d("Error",e.getMessage()+""); 137 } 138 } 139 }); 140 } 141 }); 142 builder.show(); 143 return true; 144 } 145 146 147 }
(7)详情页面:DetailActivity——获取到传递过来的信息,并加载到布局中即可。
1 package base.secret; 2 import android.app.Activity; 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.design.widget.*; 6 import android.view.Window; 7 import android.widget.TextView; 8 public class DetailActivity extends Activity { 9 private TextView textName,textPsw; 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 requestWindowFeature(Window.FEATURE_NO_TITLE); 14 setContentView(R.layout.activity_detail); 15 textName=(TextView)findViewById(R.id.text_username); 16 textPsw=(TextView)findViewById(R.id.text_psw); 17 Intent intent=getIntent(); 18 textName.setText(intent.getStringExtra("name")); 19 textPsw.setText(intent.getStringExtra("password")); 20 } 21 }
5.总结:这是第一次尝试将数据上传到云端,并从云端中获取数据,在使用bmob云端过程中,有个问题被困扰了很久,就是关于如何实现为每一个用户建立一个对应的表格,将用户的信息保存到对应的表格中,后来通过设置表名唯一,搜索表名来获取表中数据来实现,总的来说,这一次的实现过程中,处处碰壁,主要是对bmob平台的不熟悉导致,希望之后能够加强对bmob平台的熟悉,以及着手学习自己去搭建一个服务器,这样受限就不会那么多,不过这步还有很漫长,希望能坚持下去!