Simple tutorial for Android-24th guns (Baas-based user Table query function !), Androidbaas
Next, we registered several users in the previous article. The user table is as follows:
Next we will use ListView to display the data in the table!
First, let's take a look at main. xml:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <Button android: id = "@ + id/btn_query" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "query all users"/> <ListView android: id = "@ + id/lv_users" android: layout_width = "match_parent" android: layout_height = "match_parent" android: layout_below = "@ + id/btn_query"> </ListView> </RelativeLayout>
Then MainActivity. java:
Package com. example. listviewdemo; import java. util. list; import cn. bmob. v3.Bmob; import cn. bmob. v3.BmobQuery; import cn. bmob. v3.listener. findListener; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. listView; import android. widget. toast; public class MainActivi Ty extends Activity {private Button mButtonQuery; private ListView mListViewUsers; private ListViewAdapter adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Bmob. initialize (this, "8f3ffb2658d8a3316a70a0b0ca0b71b2"); // initialize BmobmButtonQuery = (Button) findViewById (R. id. btn_query); mListViewUsers = (ListView) fin DViewById (R. id. lv_users); mButtonQuery. setOnClickListener (new OnClickListenerImpl ();} private class OnClickListenerImpl implements OnClickListener {@ Overridepublic void onClick (View v) {BmobQuery <User> query = new BmobQuery <User> (); // query. findObjects (MainActivity. this, new FindListener <User> () {@ Overridepublic void onSuccess (List <User> list) {adapter = new ListViewAdapter (MainActivity. this, li St); // instantiate mListViewUsers. setAdapter (adapter) ;}@ Overridepublic void onError (int arg0, String arg1) {Toast. makeText (MainActivity. this, "query failed! ", 3). show ();}});}}}
ListViewAdapter. java:
package com.example.listviewdemo;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class ListViewAdapter extends BaseAdapter {private Context context;private List<User> list;private LayoutInflater inflater;public ListViewAdapter(Context context, List<User> list) {this.context = context;this.list = list;inflater = LayoutInflater.from(context);}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return list.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder = null;if (convertView == null) {viewHolder = new ViewHolder();convertView = inflater.inflate(R.layout.listview_item, null);viewHolder.tv_idTextView = (TextView) convertView.findViewById(R.id.tv_id);viewHolder.tv_usernameTextView = (TextView) convertView.findViewById(R.id.tv_username);viewHolder.tv_userPasswordTextView = (TextView) convertView.findViewById(R.id.tv_userpassword);viewHolder.tv_timeTextView = (TextView) convertView.findViewById(R.id.tv_time);convertView.setTag(viewHolder);} else {viewHolder = (ViewHolder) convertView.getTag();}viewHolder.tv_idTextView.setText(list.get(position).getObjectId() + "|");viewHolder.tv_usernameTextView.setText(list.get(position).getUserName()+ "|");viewHolder.tv_userPasswordTextView.setText(list.get(position).getUserPassword() + "|");viewHolder.tv_timeTextView.setText(list.get(position).getCreatedAt());return convertView;}class ViewHolder {private TextView tv_idTextView;private TextView tv_usernameTextView;private TextView tv_userPasswordTextView;private TextView tv_timeTextView;}}
List_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" android:orientation="horizontal" > <TextView android:id="@+id/tv_id" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_username" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_userpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
User. java:
package com.example.logintest;import cn.bmob.v3.BmobObject;public class User extends BmobObject {private String userPassword;public String getUserPassword() {return userPassword;}public void setUserPassword(String userPassword) {this.userPassword = userPassword;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}private String userName;}
Configuration file:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.listviewdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_LOGS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.listviewdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
Take a look at the file:
Running instance:
Thank you!
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.