Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android使用ListView將數據顯示到手機屏幕上

Android使用ListView將數據顯示到手機屏幕上

編輯:關於Android編程

下面是一個LinearLayout,裡面的內容超級簡單,就是一個ListView

 


<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">

    <listview android:id="@+id/Lv" android:layout_height="fill_parent" android:layout_width="fill_parent">
    </listview>

</linearlayout>

 

為了使程序更有擴展性,我這裡創建了一個model包,裡面包含一個Person類,就是數據庫的信息名稱

 

 

package com.neusoft.ListView.model;

public class Person {
	private String _id;
	private String name;
	private String salary;
	private String phone;

	public Person(String _id, String name, String salary, String phone) {
		super();
		this._id = _id;
		this.name = name;
		this.salary = salary;
		this.phone = phone;
	}

	@Override
	public String toString() {
		return _id + "," + name + "," + salary+ "," + phone;
	}

	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 getSalary() {
		return salary;
	}

	public void setSalary(String salary) {
		this.salary = salary;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

}
下面的這個MyOpenHelper類就是老套路了,大家都知道。他是為了創建數據庫和升級數據庫等等對數據庫的操作的類

 

 

package com.neusoft.ListView;

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, "people.db", null, 1);
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table person(_id integer primary key autoincrement,name char(20),salary char(20),phone char(20))");
	}
	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		System.out.print("數據庫升級了");

	}

}

這是MainActivity,

 

 

package com.neusoft.ListView;

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

import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.neusoft.ListView.model.*;

public class MainActivity extends Activity {
	private ListView Lv;
	List personList;//創建一個List集合,將所有數據庫中每個person加入到此集合中
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//insert();
		
		
		//從數據庫中取出數據
		personList=new ArrayList();
		MyOpenHelper oh=new MyOpenHelper(this);
		SQLiteDatabase db=oh.getWritableDatabase();

		Cursor cursor=db.query("person",null, null, null, null, null,null);
		while(cursor.moveToNext())
		{
			//循環取出每個person的值
			String _id=cursor.getString(cursor.getColumnIndex("_id"));
			String name=cursor.getString(cursor.getColumnIndex("name"));
			String salary=cursor.getString(cursor.getColumnIndex("salary"));
			String phone=cursor.getString(cursor.getColumnIndex("phone"));
			Person p=new Person(_id, name, salary, phone);
			personList.add(p);//將值加入到集合中
		}
		Lv=(ListView) findViewById(R.id.Lv);
		//將數據顯示到屏幕
		Lv.setAdapter(new MyAdapter());
		
	}
	//創建一個內部類MyAdapter,適配器,就是將數據顯示到屏幕的工具。
	class MyAdapter extends BaseAdapter
	{

		@Override
		public int getCount() {
			return personList.size();
		}

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

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

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			TextView tv=new TextView(MainActivity.this);
			Person person=personList.get(arg0);
			tv.setText(person.toString());
			
			return tv;
		}
		
	}
	//剛開始插入的數據
	public void insert()
	{
		MyOpenHelper oh=new MyOpenHelper(this);
		SQLiteDatabase db=oh.getWritableDatabase();
		for(int i=0;i<100;i++)
		{
			ContentValues values=new ContentValues();
			values.put("name", "張宇翔"+i);
			values.put("salary", "1500"+i);
			values.put("phone","13408"+i);
			db.insert("person", null, values);
		}
	}
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved