Android-使用ListView把SQLite中的数据显示至屏幕

结果图:

Android-使用ListView把SQLite中的数据显示至屏幕_第1张图片


步骤:

1.向数据库中插入数据

2.从数据库中取出数据放在ArrayList中;

3.将ArrayList中的数据显示在ListView中;


布局文件:

activity_main.xml



    
    

listitem_layout.xml




    

    
        
        
    


Java文件:
Person.java

package com.example.cn;

public class Person {

	private String _id;
	private String name;
	private String phone;
	private String salary;
	
	public String get_id() {
		return _id;
	}
	public void set_id(String _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;
	}
	public String getSalary() {
		return salary;
	}
	public void setSalary(String salary) {
		this.salary = salary;
	}
	@Override
	public String toString() {
		return name + ", " + phone + ", " + salary;
	}
	public Person(String _id, String name, String phone, String salary) {
		super();
		this._id = _id;
		this.name = name;
		this.phone = phone;
		this.salary = salary;
	}
	
	
}
MyOpenHelper.java

package com.example.android04;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MyOpenHelper extends SQLiteOpenHelper {

	public MyOpenHelper(Context context) {
		//创建数据库
		super(context, "person.db", null, 1);
		// TODO Auto-generated constructor stub
		System.out.println("MyOpenHelper");
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		//创建表
		db.execSQL("create table person(_id integer primary key autoincrement, name char(10), salary char(20), phone integer(20) )");

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

}

MainActivity.java

package com.example.android04;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.cn.Person;

public class MainActivity extends Activity {

	List personList;
	MyOpenHelper mOpenHelper;
	SQLiteDatabase db;
	MyAdapter myAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ListView lv = (ListView) findViewById(R.id.listView);
		personList = new ArrayList();
		// 创建MyOpenHelper实例
		mOpenHelper = new MyOpenHelper(this);
		// 得到数据库
		db = mOpenHelper.getWritableDatabase();
		// 插入数据
		Insert();
		// 查询数据
		Query();
		// 创建MyAdapter实例
		myAdapter = new MyAdapter(this);
		// 向listview中添加Adapter
		lv.setAdapter(myAdapter);
	}

	// 创建MyAdapter继承BaseAdapter
	class MyAdapter extends BaseAdapter {
		private Context context;
		private LayoutInflater inflater;

		public MyAdapter(Context context) {
			this.context = context;
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return personList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// 从personList取出Person
			Person p = personList.get(position);
			ViewHolder viewHolder = null;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = inflater.inflate(R.layout.listitem_layout, null);
				viewHolder.txt_name = (TextView) convertView
						.findViewById(R.id.tv_name);
				viewHolder.txt_phone = (TextView) convertView
						.findViewById(R.id.tv_phone);
				viewHolder.txt_salary = (TextView) convertView
						.findViewById(R.id.tv_salary);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			//向TextView中插入数据
			viewHolder.txt_name.setText(p.getName());
			viewHolder.txt_phone.setText(p.getPhone());
			viewHolder.txt_salary.setText(p.getSalary());

			return convertView;
		}
	}

	class ViewHolder {
		private TextView txt_name;
		private TextView txt_phone;
		private TextView txt_salary;
	}

	// 插入数据
	public void Insert() {
		for (int i = 0; i < 100; i++) {
			ContentValues values = new ContentValues();
			values.put("name", "张三" + i);
			values.put("salary", "123" + i + i);
			values.put("phone", "151" + i + i);
			db.insert("person", null, values);
		}
	}

	// 查询数据
	public void Query() {
		Cursor cursor = db.query("person", null, null, null, null, null, null);
		while (cursor.moveToNext()) {
			String _id = cursor.getString(0);
			String name = cursor.getString(1);
			String salary = cursor.getString(2);
			String phone = cursor.getString(3);
			Person person = new Person(_id, name, phone, salary);
			personList.add(person);
		}
	}

}








你可能感兴趣的:(Android-基础)