編輯:關於android開發
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>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 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 }View Code
(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 }View Code
(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 }View Code
(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 }View Code
(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 }View Code
(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 }View Code
(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 }View Code
5.總結:這是第一次嘗試將數據上傳到雲端,並從雲端中獲取數據,在使用bmob雲端過程中,有個問題被困擾了很久,就是關於如何實現為每一個用戶建立一個對應的表格,將用戶的信息保存到對應的表格中,後來通過設置表名唯一,搜索表名來獲取表中數據來實現,總的來說,這一次的實現過程中,處處碰壁,主要是對bmob平台的不熟悉導致,希望之後能夠加強對bmob平台的熟悉,以及著手學習自己去搭建一個服務器,這樣受限就不會那麼多,不過這步還有很漫長,希望能堅持下去!
[Android學習筆記]Adapter如何進行視圖與數據綁定資料 Android開發中視圖和數據的綁定離不開Adapt系列
Android接入微信支付完全解析,太全了~ 今天來聊聊,android中接入微信支付的需求,肯定有人會說,這多簡單呀,還在這裡扯什麼,趕快去洗洗睡吧~~ 那我就不
對於任何軟件來說,美觀的界面都是用戶體驗的重要組成部分,它能提高整個軟件的品質
React-Native系列Android——Touch事件原理及狀態效果 Native原生相比於Hybrid或H5最大優點是具有流暢和復雜的交互效果,觸摸事件便是其
Android開發4: Notification編程基礎、Broadca