Android eclipse实现创建水果表并添加信息,将其以ListView控件显示
整个程序包含:三个.java文件以及两个.xml文件
1.Fruit.java:
package com.gpnu.zhr;
//水果信息包含水果的编号,水果的名字以及水果的颜色。
public class Fruit {
private int id;
private String fruit_name;
private String fruit_color;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFruit_name() {
return fruit_name;
}
public void setFruit_name(String fruit_name) {
this.fruit_name = fruit_name;
}
public String getFruit_color() {
return fruit_color;
}
public void setFruit_color(String fruit_color) {
this.fruit_color = fruit_color;
}
public String toString() {
return "Fruit [id=" + id + ", fruit_name=" + fruit_name + ", fruit_color=" + fruit_color + "]";
}
/*
public Fruit (long id,String fruit_name,String fruit_color){
super();
this.id = id;
this.fruit_name = fruit_name;
this.fruit_color = fruit_color;
}
*/
}
2.MyHelper.java:
package com.gpnu.zhr;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyHelper extends SQLiteOpenHelper{
public MyHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//System.out.println("oncreate数据库被第一次创建了");
// 数据表结构
db.execSQL("create table FruitInfo (_id integer primary key autoincrement, fruit_name varchar(20), fruit_color varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
System.out.println("onUpgrade被调用了");
}
}
3.MainActivity.java:
package com.gpnu.zhr;
import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import