时间:2021-07-01 10:21:17 帮助过:34人阅读
java文件
Girl.java
作为javabean,存储数据类用的
package com.example.listviewdemo; public class Girl { private String name; private int age; private String phone; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return name+","+age+","+phone; } public Girl(String name, int age, String phone) { super(); this.name = name; this.age = age; this.phone = phone; } }
MainActivity.java
package com.example.listviewdemo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private MyOpenSqlHelp myDatebaseHelper; List<Girl> girList ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myDatebaseHelper = new MyOpenSqlHelp(this, "People.db", null, 1); SQLiteDatabase db = myDatebaseHelper.getWritableDatabase(); girList = new ArrayList<Girl>(); } public void Create_Data(View v) { SQLiteDatabase db = myDatebaseHelper.getWritableDatabase(); for(int i=0;i<=50;i++){ ContentValues values = new ContentValues(); values.put("Name", "朱主爱"+i); values.put("Age", "19"); values.put("Phone", "135708"+i); db.insert("Girl", null, values); } Toast.makeText(this, "数据创建成功", 0).show(); } public void Query_Data(View v) { SQLiteDatabase db = myDatebaseHelper.getWritableDatabase(); Cursor cursor = db.query("Girl", null, null, null, null, null, null); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("Name")); int age = cursor.getInt(cursor.getColumnIndex("Age")); String phone = cursor.getString(cursor.getColumnIndex("Phone")); Girl girl =new Girl(name,age,phone); girList.add(girl); } //拿到listveiw对象 ListView lv = (ListView) findViewById(R.id.lv); //设置适配器 lv.setAdapter(new MyAapter()); } //适配器类 class MyAapter extends BaseAdapter { //获取集合中有多少条元素,由系统调用 @Override public int getCount() { // TODO Auto-generated method stub return girList.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; } //由系统调用,返回一个view对象作为listview的条目 /* * position:本次getView方法调用所返回的view对象在listView中处于第几个条目,position的值就为多少 * */ @Override public View getView(int position, View convertView, ViewGroup parent) { TextView tv = new TextView(MainActivity.this); tv.setTextSize(18); //获取集合中的元素 Girl girl = girList.get(position); tv.setText(girl.toString()); return tv; } } }
MyOpenSqlHelp.java
数据库帮助器,要用sqlite数据库必须创建的类
package com.example.listviewdemo; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.widget.Toast; public class MyOpenSqlHelp extends SQLiteOpenHelper{ public static final String CREATE_GIRL = "create table Girl(_id integer primary key autoincrement,Name char(20),Age integer,Phone char(20))" ; private Context mContext; public MyOpenSqlHelp(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub mContext = context ; } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(CREATE_GIRL); /*Toast.makeText(mContext,"Create Successded",0).show();*/ } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
ListView显示Sqlite的数据
标签: