如果你想显示每个人的每一个信息
dog
在一个
ListView
,您需要
Dog
班虽然这不是必需的,但它会使您的工作更轻松,而且从数据库中获取数据并存储每个
狗
在一个
狗
类实例。
public class Dog {
private String id;
private String name;
private String age;
private String breed;
private String weight;
public Dog(String id, String name, String age, String breed, String weight) {
this.id = id;
this.name = name;
this.age = age;
this.breed = breed;
this.weight = weight;
}
public String getID() {
return this.id;
}
public String getName() {
return this.name;
}
public String getAge() {
return this.age;
}
public String getWeight() {
return this.weight;
}
public String getBreed() {
return this.breed;
}
}
然后需要定义
layout XML
将表示
列表视图
. 您可以按照自己的方式进行设计。
这是一个
实例
行布局代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/text_dogID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="demo"
android:textColor="#000"
android:textSize="25sp"/>
<TextView
android:id="@+id/text_dogName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="demo"
android:textColor="#000"
android:textSize="25sp"
app:layout_constraintTop_toBottomOf="@id/text_dogID"/>
<TextView
android:id="@+id/text_dogAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="demo"
android:textColor="#000"
android:textSize="25sp"
app:layout_constraintTop_toBottomOf="@id/text_dogName"/>
<TextView
android:id="@+id/text_dogWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="demo"
android:textColor="#000"
android:textSize="25sp"
app:layout_constraintTop_toBottomOf="@id/text_dogAge"/>
<TextView
android:id="@+id/text_dogBreed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="demo"
android:textColor="#000"
android:textSize="25sp"
app:layout_constraintTop_toBottomOf="@id/text_dogWeight"/>
</android.support.constraint.ConstraintLayout>
之后,你需要自己的
custom adapter
将其分类
extends
这个
BaseAdapter
类别和
Override
这个
getView
方法
这是一个
实例
自定义适配器类
public class CustomAdapter extends BaseAdapter{
private ArrayList<Dog> dogsList;
private Context context;
public CustomAdapter(ArrayList<Dog> list, Context cont){
this.dogsList = list;
this.context = cont;
}
@Override
public int getCount() {
return this.dogsList.size();
}
@Override
public Object getItem(int position) {
return this.dogsList.get(position);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
LayoutInflater inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.listview_row_layout, null);
holder = new ViewHolder();
holder.id = (TextView)convertView.findViewById(R.id.text_dogID);
holder.name = (TextView)convertView.findViewById(R.id.text_dogName);
holder.age = (TextView)convertView.findViewById(R.id.text_dogAge);
holder.weight = (TextView)convertView.findViewById(R.id.text_dogWeight);
holder.breed = (TextView)convertView.findViewById(R.id.text_dogBreed);
convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
Dog stu = dogsList.get(position);
holder.id.setText(stu.getID());
holder.name.setText(stu.getName());
holder.age.setText(stu.getAge());
holder.weight.setText(stu.getWeight());
holder.breed.setText(stu.getBreed());
return convertView;
}
private static class ViewHolder{
public TextView id;
public TextView name;
public TextView age;
public TextView weight;
public TextView breed;
}
}
现在,您需要从数据库中获取数据,并将每只狗的信息存储在单独的
狗
前面创建的类实例。
public ArrayList<Dog> getAllData() {
ArrayList<Dog> doglist = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
while(res.moveToNext()) {
String id = res.getString(0); //0 is the number of id column in your database table
String name = res.getString(1);
String age = res.getString(2);
String breed = res.getString(3);
String weight = res.getString(4);
Dog newDog = new Dog(id, name, age, breed, weight);
doglist.add(newDog);
}
return doglist;
}
现在,数据库中的所有数据都存储在
狗
存储在中的类
doglist
ArrayList
.
最后,您需要一种方法来填充
列表视图
public void fillListview() {
ListView myListview = findViewById(R.id.myListview);
DatabaseHelper dbhelper = new DatabaseHelper(this);
ArrayList<Dog> dogList = dbhelper.getAllData();
Customadapter myAdapter = new Customadapter(dogList, this);
myListview.setAdapter(myAdapter);
}