android查询mysql并显示_Android实现查询数据库,把数据库内容显示到屏幕

首先,是布局文件,非常的简单,一个可以滑动的ScrollView包裹一个线性布局,线性布局里面无内容

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/ll"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity" >

为了方面使用,我们定义一个person类,这个类中包含了我们需要查询的那个数据库的信息名称。

package com.neusoft.show.domal;

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;

}

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;

}

@Override

public String toString() {

return _id + "," + name + ", " + salary+ ", " + phone ;

}

}

接下来,再来一个MyOpenHelper类,这个类用来创建数据库和升级数据库用的

package com.neusoft.show;

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) {

// TODO Auto-generated method stub

System.out.print("数据库升级了!!!");

}

}

最后一步,就是把数据库中的内容显示到手机的屏幕上。理所应当,我们应该查询数据库,然后得到值,再把它显示到手机屏幕上。这里由于刚开始学,所以,我们用的最简单的TextView,就没用ListView了。我的思路是,每有一个数据,就创建一个TextView,然后把这些TextView插入到一个List集合中,最后循环显示到界面。

package com.neusoft.show;

import java.util.ArrayList;

import java.util.List;

import com.neusoft.show.domal.Person;

import android.R.integer;

import android.os.Bundle;

import android.app.Activity;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.view.Menu;

import android.widget.LinearLayout;

import android.widget.TextView;

public class MainActivity extends Activity {

List personList;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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()) {

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);

}

LinearLayout ll=(LinearLayout) findViewById(R.id.ll);

//把数据显示到屏幕

for(Person p:personList)

{

//1.集合中每有一条数据,就new一个TextView

TextView tv=new TextView(this);

//2.把人物的信息设置为文本的内容

tv.setText(p.toString());

tv.setTextSize(18);

//3.把TextView设置成线性布局的子节点

ll.addView(tv);

}

}

}

原文:http://blog.csdn.net/zyx520ytt/article/details/51346886

你可能感兴趣的:(android查询mysql并显示_Android实现查询数据库,把数据库内容显示到屏幕)