listview小例子
程序员文章站
2022-05-31 08:36:13
...
编写一个从数据库里查询数据展示到listview上的案例
运行结果:点击find按钮,会将数据库中添加的数据显示在List View上
结果如下:
创建如图所示:
代码如下:
PersonAdapter
package cn.edu.bzu.datashow.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import java.util.List; import cn.edu.bzu.datashow.R; import cn.edu.bzu.datashow.entity.Person; public class PersonAdapter extends ArrayAdapter<Person> { private int resourceId; public PersonAdapter(Context context, int textViewResourceId, List<Person> objects) { super(context, textViewResourceId, objects); resourceId = textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { Person person= getItem(position); View view; ViewHolder viewHolder; if (convertView == null) { //任务 补充完整 view = LayoutInflater.from(getContext()).inflate(resourceId, null); viewHolder = new ViewHolder(); viewHolder.tvName = (TextView) view.findViewById(R.id.tvName); viewHolder.tvPhone = (TextView) view.findViewById(R.id.tvPhone); view.setTag(viewHolder); } else { view = convertView; viewHolder = (ViewHolder) view.getTag(); } viewHolder.tvName.setText(person.getName()); viewHolder.tvPhone.setText(person.getPhone()); return view; } class ViewHolder { TextView tvName; TextView tvPhone; } }
PersonDao:
package cn.edu.bzu.datashow.dao; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import java.util.ArrayList; import java.util.List; import cn.edu.bzu.datashow.db.PersonDBHelper; import cn.edu.bzu.datashow.entity.Person; /** * Created by fengj on 2017/5/10. */ public class PersonDao { private PersonDBHelper personDBHelper; public PersonDao(Context context){ personDBHelper=new PersonDBHelper(context); } public void add(Person person){ SQLiteDatabase sqLiteDatabase=personDBHelper.getWritableDatabase(); ContentValues contentValues=new ContentValues(); contentValues.put("name",person.getName()); contentValues.put("phone",person.getPhone()); sqLiteDatabase.insert("info",null,contentValues); } public List<Person> getPersons(){ //任务 完成查询所有数据的方法 List<Person> persons=new ArrayList<Person>(); SQLiteDatabase sqLiteDatabase=personDBHelper.getWritableDatabase(); Cursor cursor=sqLiteDatabase.query("info",null,null,null,null,null,null); while(cursor.moveToNext()){ int id= cursor.getInt(cursor.getColumnIndex("_id")); String name=cursor.getString(cursor.getColumnIndex("name")); String phone=cursor.getString(cursor.getColumnIndex("phone")); Person person=new Person(name,phone); person.setId(id); persons.add(person); } return persons; } }PersonDBHelper:
package cn.edu.bzu.datashow.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * Created by fengj on 2017/5/10. */ //任务:修正问题 public class PersonDBHelper extends SQLiteOpenHelper { //数据库的名称为person.db ,数据库的版本为1 public PersonDBHelper(Context context) { super(context,"person.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table info(_id integer primary key,name varchar(20),phone varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }MainActivity:
package cn.edu.bzu.datashow; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ListView; import java.util.List; import cn.edu.bzu.datashow.adapter.PersonAdapter; import cn.edu.bzu.datashow.dao.PersonDao; import cn.edu.bzu.datashow.entity.Person; public class MainActivity extends AppCompatActivity { private PersonDao personDao; private ListView lvPersons; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lvPersons= (ListView) findViewById(R.id.lvPersons); personDao=new PersonDao(this); personDao.add(new Person("张三","13476089006")); personDao.add(new Person("李四","13476089008")); } public void find(View view){ //任务 查询数据库中的所有person,显示在列表中 List<Person> persons=personDao.getPersons(); PersonAdapter personAdapter=new PersonAdapter(this,R.layout.item,persons); lvPersons.setAdapter(personAdapter); } }Activity-main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="cn.edu.bzu.datashow.MainActivity"> <ListView android:id="@+id/lvPersons" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@color/colorPrimary" android:layout_weight="1" /> <Button android:text="Find" android:layout_gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/lvPersons" android:onClick="find" android:id="@+id/btnFind" /> </LinearLayout>item.xml:
<?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:id="@+id/activity_shop" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" > <TextView android:text="名字" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvName" android:textSize="24sp" /> <TextView android:text="电话" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:id="@+id/tvPhone" /> </LinearLayout>运行出来就好啦!
</div>