SQLite数据库--查询数据展示到listview上的案例

编写一个从数据库里查询数据展示到listview上的案例

运行结果:点击find按钮,会将数据库中添加的数据显示在List View

SQLite数据库--查询数据展示到listview上的案例_第1张图片     SQLite数据库--查询数据展示到listview上的案例_第2张图片

1.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 {
	private int resourceId;
	public PersonAdapter(Context context, int textViewResourceId,
						 List 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;

	}
}
2.PersonDao.java代码:

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;

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 getPersons(){
        //任务 完成查询所有数据的方法
        List persons=new ArrayList();
        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;
    }
}
3.PersonDBHelper.java代码:

package cn.edu.bzu.datashow.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
//任务:修正问题
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) {

    }
}
4.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 persons=personDao.getPersons();
        PersonAdapter personAdapter=new PersonAdapter(this,R.layout.item,persons);
        lvPersons.setAdapter(personAdapter);


    }
}
5.activity_main.xml代码:




    

    
6.item.xml代码:



    

    


测验的时候并没有做出来,主要还是不理解。特别是 ArrayAdapter适配器。






你可能感兴趣的:(SQLite数据库--查询数据展示到listview上的案例)