一、熟悉Bmob
1、Bmob官方地址
2、查看Bmob關於android快速入門文檔:鏈接
二、瞭解ListView
1、構建泛類
2、構建適配器
3、構建適配器的layout
4、在活動中啓動ListView
三、源碼
泛類Person
public class Person extends BmobObject {
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
適配器personAdapte
public class PersonAdapte extends ArrayAdapter<Person>{
private int resourceId;
public PersonAdapte(Context context, int resource, List<Person> objects) {
super(context, resource,objects);
resourceId =resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Person person = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
TextView textView1 = view.findViewById(R.id.textView1);
TextView textView2 = view.findViewById(R.id.textView2);
textView1.setText(person.getName());
textView2.setText(person.getAddress());
return view;
}
}
適配器Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="243dp"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="155dp"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
活動源碼
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 啓動數據庫
Bmob.initialize(this, "填你的Bmob應用ID");
// 從Bmob查找數據,在ListView顯示
BmobQuery<Person> bmobQuery = new BmobQuery<Person>();
bmobQuery.findObjects(new FindListener<Person>() {
@Override
public void done(List<Person> list, BmobException e) {
if(e==null){
Log.d("path","查詢成功");
PersonAdapte personAdapte = new PersonAdapte(MainActivity.this,R.layout.personlayout,list);
ListView listView = findViewById(R.id.listView);
listView.setAdapter(personAdapte);
}
else{
Log.d("path","查詢不成功");
}
}
});
}
}
活動layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>