数据库里查询数据
ListView控件是Android开发里一个比较常用的控件,它以列表的形式展示具体数据内容,并且能够根据数据的长度自适应屏幕显示。因此,以下这个案例就是运用ListView的常见属性来实现各项功能。
程序对应的布局文件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>
结果图如下:
在Java里边建立四个包,分别为:adapter、dao、db、entity
在entity包下创建一个Person类,其代码如下:
package cn.edu.bzu.datashow.entity;
/**
* Created by fengj on 2017/5/10.
*/
public class Person {
private int id;
private String name;
private String phone;
//任务:生成构造方法 参数 name,phone
public Person(String name, String phone) {
this.name = name;
this.phone = phone;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
在db包下创建一个PersonDBHelper.java类,代码如下所示:
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) {
}
}
在dao包下创建一个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;
}
}
在adapter包下创建一个PersonAdapter.java类,代码如下:
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;
}
}
最后编写交互代码,数据库的操作完成之后需要完成界面与数据库的交互,用于实现将数据库中的数据以ListView的形式展现在界面上,MainActivity.java的代码如下:
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);
}
}
点击一下find,则回跳转到另一个界面: