andoird软件开发之一个记录账号密码的APP--bmob后台

摘要:
2.实现的效果图,如下:以下界面分别为注册界面、登录界面、提交账户内容界面、账户列表界面、长按删除账户信息、具体账户内容信息3.实现的工程目录如下:4.实现的具体过程:a.布局代码:登录界面图:activity_main.xml1˂?

1.app功能:通过注册登录账户,拥有一个账户本,能够将平时自己容易的忘记的账户记录下来,并可以保持到云端,不需要担心数据丢失,只要登录账户,便可获取到自己的账户本。

2.实现的效果图,如下:

以下界面分别为注册界面、登录界面、提交账户内容界面、账户列表界面、长按删除账户信息、具体账户内容信息

andoird软件开发之一个记录账号密码的APP--bmob后台第1张andoird软件开发之一个记录账号密码的APP--bmob后台第2张andoird软件开发之一个记录账号密码的APP--bmob后台第3张

andoird软件开发之一个记录账号密码的APP--bmob后台第4张andoird软件开发之一个记录账号密码的APP--bmob后台第5张andoird软件开发之一个记录账号密码的APP--bmob后台第6张

3.实现的工程目录如下:

andoird软件开发之一个记录账号密码的APP--bmob后台第7张

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>
View Code

(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>
View Code

(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>
View Code

(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>
View Code

(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>
View Code

(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>
View Code

b.实现代码:

(1)主界面:MainActivity——主要是实现跳转页面的功能和利用BmobUser对象判断用户名登录是否正确:

1 packagebase.secret;
2 
3 importandroid.app.Activity;
4 importandroid.content.Intent;
5 importandroid.os.Bundle;
6 importandroid.support.annotation.MainThread;
7 import android.support.design.widget.*;
8 importandroid.support.design.widget.TextInputLayout;
9 importandroid.view.View;
10 importandroid.view.Window;
11 importandroid.widget.Toast;
12 
13 importcn.bmob.v3.Bmob;
14 importcn.bmob.v3.BmobQuery;
15 importcn.bmob.v3.BmobUser;
16 importcn.bmob.v3.exception.BmobException;
17 importcn.bmob.v3.listener.SaveListener;
18 
19 public class MainActivity extends Activity implementsView.OnClickListener {
20     privateandroid.support.design.widget.TextInputLayout editName,editPsw;
21 //此处定义自己app的ID
22     private String ID="";
23 @Override
24     protected voidonCreate(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 voidonClick(View v) {
37         switch(v.getId()){
38             caseR.id.btn_login:
39                 editName.setError("");
40                 editPsw.setError("");
41 Login();
42                 break;
43             caseR.id.btn_register:
44                 Intent intentRegister=new Intent(MainActivity.this,RegisterActivity.class);
45 startActivity(intentRegister);
46                 break;
47 }
48 }
49     private voidLogin(){
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=newBmobUser();
63 user.setUsername(name);
64 user.setPassword(psw);
65         user.login(new SaveListener<BmobUser>() {
66 @Override
67             public voiddone(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 }
View Code

(2)注册界面:RegisterActivity——主要实现用户名注册,获取到用户输入的数据,并上传到云端

1 packagebase.secret;
2 
3 importandroid.app.Activity;
4 importandroid.content.Intent;
5 importandroid.os.Bundle;
6 importandroid.support.design.widget.TextInputLayout;
7 importandroid.view.View;
8 importandroid.view.Window;
9 importandroid.widget.Toast;
10 
11 importcn.bmob.v3.Bmob;
12 importcn.bmob.v3.BmobUser;
13 importcn.bmob.v3.exception.BmobException;
14 importcn.bmob.v3.listener.SaveListener;
15 
16 
17 public class RegisterActivity extends Activity implementsView.OnClickListener {
18     private String ID="";
19     privateandroid.support.design.widget.TextInputLayout editName,editPsw,editAgainPsw;
20 @Override
21     protected voidonCreate(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 voidonClick(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 voidstoreUserMessage(String name,String psw){
43         //保存用户信息到云端
44         BmobUser user=newBmobUser();
45 user.setUsername(name);
46 user.setPassword(psw);
47         user.signUp(new SaveListener<BmobUser>() {
48 @Override
49             public voiddone(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 booleanisValid(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 }
View Code

(3)建立一个UserMessage类来保存数据,主要含有用户名,密码和相关信息,因为要将数据上传到云端,所以该类要继承bmob的一个BmobObject对象。

1 packagebase.secret;
2 importcn.bmob.v3.BmobObject;
3 public class UserMessage extendsBmobObject {
4     privateString user;
5     privateString psw;
6     privateString message;
7     publicUserMessage(){
8 }
9     publicUserMessage(String user, String psw, String message) {
10         this.user =user;
11         this.psw =psw;
12         this.message =message;
13 }
14     publicUserMessage(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     publicString getUser() {
21         returnuser;
22 }
23     public voidsetUser(String user) {
24         this.user =user;
25 }
26     publicString getPsw() {
27         returnpsw;
28 }
29     public voidsetPsw(String psw) {
30         this.psw =psw;
31 }
32     publicString getMessage() {
33         returnmessage;
34 }
35     public voidsetMessage(String message) {
36         this.message =message;
37 }
38 }
View Code

(4)建立完后,我们要自定义一个适配器UserAdapter去加载数据到列表界面中:

1 packagebase.secret;
2 
3 importandroid.content.Context;
4 importandroid.view.LayoutInflater;
5 importandroid.view.View;
6 importandroid.view.ViewGroup;
7 importandroid.widget.ArrayAdapter;
8 importandroid.widget.TextView;
9 importjava.util.List;
10 public class UserAdapter extends ArrayAdapter<UserMessage>{
11     private  intresourceId;
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(intposition, 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         returnview;
25 }
26 }
View Code

(5)添加账户界面:AddActivity——主要实现获取到用户输入的信息,然后保存到云端中,这里涉及到一个bmob的使用问题,按理我们应该为每个用户都创建一个表来存储用户的信息,为了更好地从bmob云端获取到数据,我这里设置用户名作为表名,而且设置了用户名为唯一键,即不可重复。

1 packagebase.secret;
2 importandroid.app.Activity;
3 importandroid.content.Intent;
4 importandroid.os.Bundle;
5 import android.support.design.widget.*;
6 importandroid.support.design.widget.TextInputLayout;
7 importandroid.util.Log;
8 importandroid.view.View;
9 importandroid.view.Window;
10 importandroid.widget.Toast;
11 
12 importjava.util.List;
13 
14 importcn.bmob.v3.Bmob;
15 importcn.bmob.v3.BmobObject;
16 importcn.bmob.v3.BmobUser;
17 importcn.bmob.v3.exception.BmobException;
18 importcn.bmob.v3.listener.SaveListener;
19 importcn.bmob.v3.listener.UpdateListener;
20 public class AddActivity extends Activity implementsView.OnClickListener {
21     private String ID="";
22     privateandroid.support.design.widget.TextInputLayout editMessage,editUsername,editPassword;
23 @Override
24     protected voidonCreate(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 voidonClick(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=newUserMessage(str,user,psw,content);
54             userMessage.save(new SaveListener<String>() {
55 @Override
56                 public voiddone(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 }
View Code

(6)添加完信息后,我们应该将在列表界面中更新出相关数据出来,那么我们去更新数据,即从bmob云端中获取数据,那么这里要去搜索与用户相对应的信息,而这里我们直接去搜索表名为用户名的表格即可,因为我们之前已经设置了表名为用户名,但这里要注意的是,从bmob云端中获取到的数据是json类型数据,所以我们还要对其进行解析。以及长按删除列表项的功能和点击跳转到详情页面。

1 packagebase.secret;
2 
3 importandroid.app.Activity;
4 importandroid.content.DialogInterface;
5 importandroid.content.Intent;
6 importandroid.os.Bundle;
7 importandroid.support.v7.app.AlertDialog;
8 importandroid.util.Log;
9 importandroid.view.View;
10 importandroid.view.Window;
11 importandroid.widget.AdapterView;
12 importandroid.widget.ListView;
13 importandroid.widget.Toast;
14 
15 importorg.json.JSONArray;
16 importorg.json.JSONException;
17 importorg.json.JSONObject;
18 
19 importjava.util.ArrayList;
20 importjava.util.List;
21 
22 importcn.bmob.v3.Bmob;
23 importcn.bmob.v3.BmobQuery;
24 importcn.bmob.v3.BmobUser;
25 importcn.bmob.v3.exception.BmobException;
26 importcn.bmob.v3.listener.FindListener;
27 importcn.bmob.v3.listener.QueryListener;
28 importcn.bmob.v3.listener.UpdateListener;
29 public class ListActivity extends Activity implementsView.OnClickListener, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
30     private List<UserMessage> listContent=new ArrayList<>();
31     privateListView listView;
32     private String ID="";
33     privateUserAdapter adapter;
34 @Override
35     protected voidonCreate(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 voidgetData() {
50         //获取到当前用户名
51         BmobUser user=BmobUser.getCurrentUser();
52         //搜索bmob中表名与用户名等同的表
53         BmobQuery query=newBmobQuery(user.getUsername());
54         query.findObjectsByTable(new QueryListener<JSONArray>() {
55 @Override
56             public voiddone(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=newUserMessage(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 voidonClick(View v) {
88         switch(v.getId()){
89             caseR.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, longid) {
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, longid) {
110         //实现长按删除功能
111         AlertDialog.Builder builder=new AlertDialog.Builder(ListActivity.this);
112         builder.setMessage("是否要删除该内容?").setNegativeButton("取消", newDialogInterface.OnClickListener() {
113 @Override
114             public void onClick(DialogInterface dialog, intwhich) {
115 
116 }
117         }).setPositiveButton("确定", newDialogInterface.OnClickListener() {
118 @Override
119             public void onClick(DialogInterface dialog, intwhich) {
120                 //获取到要删除的表
121                 BmobUser bmobUser=BmobUser.getCurrentUser();
122                 UserMessage user=newUserMessage();
123 user.setTableName(bmobUser.getUsername());
124                 //删除在云端的信息
125                 user.delete(listContent.get(position).getObjectId(), newUpdateListener() {
126 @Override
127                     public voiddone(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 }
View Code

(7)详情页面:DetailActivity——获取到传递过来的信息,并加载到布局中即可。

1 packagebase.secret;
2 importandroid.app.Activity;
3 importandroid.content.Intent;
4 importandroid.os.Bundle;
5 import android.support.design.widget.*;
6 importandroid.view.Window;
7 importandroid.widget.TextView;
8 public class DetailActivity extendsActivity {
9     privateTextView textName,textPsw;
10 @Override
11     protected voidonCreate(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 }
View Code

5.总结:这是第一次尝试将数据上传到云端,并从云端中获取数据,在使用bmob云端过程中,有个问题被困扰了很久,就是关于如何实现为每一个用户建立一个对应的表格,将用户的信息保存到对应的表格中,后来通过设置表名唯一,搜索表名来获取表中数据来实现,总的来说,这一次的实现过程中,处处碰壁,主要是对bmob平台的不熟悉导致,希望之后能够加强对bmob平台的熟悉,以及着手学习自己去搭建一个服务器,这样受限就不会那么多,不过这步还有很漫长,希望能坚持下去!

免责声明:文章转载自《andoird软件开发之一个记录账号密码的APP--bmob后台》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Markdown基础教程星球大战 BZOJ 1015下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

读懂IL代码就这么简单(二)

一 前言   IL系列 第一篇写完后 得到高人指点,及时更正了文章中的错误,也使得我写这篇文章时更加谨慎,自己在了解相关知识点时,也更为细致。个人觉得既然做为文章写出来,就一定要保证比较高的质量,和正确率 。感谢 @冰麟轻武 的指点 你没有看第一篇?  点这里看第一篇 读懂IL代码就这么简单(一)   IL指令大全 :IL指令详解 IL反编译工具: ILD...

java爬虫(四)利用Jsoup获取需要登陆的网站中的内容(无验证码的登录)

一、实现原理 登录之后进行数据分析,精确抓取数据。根据上篇文章的代码,我们不仅获取了cookies,还获取了登录之后返回的网页源码,此时有如下几种种情况:(1)若我们所需的数据就在登录之后返回的源码里面,那么我们就可以直接通过Jsoup去解析源码了,然后利用Jsoup的选择器功能去筛选出我们需要的信息;(2)若需要的数据是需要通过请求源码里的链接得到,那么...

iOS开发 字符串

iOS开发之玩转字符串 (转)这是另一个收藏的超链接 来自SegmentFault 大神的精品 下面这是一些常用的方法和属性 记得不牢所以收藏下 //一、NSString /*----------------创建字符串的方法----------------*///1、创建常量字符串。NSString *astring = @"This is a Strin...

使用LitJson解析json信息

1.载入LitJson.dll文件 2.解析的主要代码 TextAsset textAsset= Resources.Load<TextAsset>("UIPanelJson"); //载入json文件 JsonData jsonData = JsonMapper.ToObject(textAsset.text); //将json信息...

在 foreach 中操作集合时报错:Collection was modified; enumeration operation may not execute.

错误信息:System.InvalidOperationException: Collection was modified; enumeration operation may not execute. 在 foreach 操作集合时就有可能遇到这个错误提示。下面将描述场景并提供解决方案: var dict = new Dictionary<str...

Java中关于Date等日期类的简单使用

Date Date类不常用,很多方法被废弃了,常用它的两个构造方法来new一个Date对象。 Date d1 = new Date(); //不传任何参数,代表当前时间点 System.out.println(d1); //输出 Sat Jul 13 09:51:50 CST 2019 //在Java中以1970年1月1日 00:00:00为时间...