ListView and Sqlite Database

来源:互联网 发布:软件生命周期 编辑:程序博客网 时间:2024/05/19 04:04

距离上一次更新博客距离九天,这一次,写了个将Sqlite Database里的数据显示在ListView上的小活动,也算是初步认识了ListView控件吧。下面贴下代码

HistoryList类

我在数据库里存储的计算器的每次计算过程,如:1+2=3……
首先创建HistoryList类

public class HistoryList {    private String first,op1,second,op2,third;    public HistoryList(String first, String op1, String second, String op2, String third)    {        this.first=first;        this.op1=op1;        this.second=second;        this.op2=op2;        this.third=third;    }    public String get_first(){        return first;    }    public String get_op1(){        return op1;    }    public String get_second(){        return second;    }    public String get_op2(){        return op2;    }    public String get_third(){        return third;    }}

里面的各个函数相信大家都看得懂。就不细说了

创建HistoryAdpter适配器

要想把Sqlite Database里的数据显示到ListView上,首先要自定义一个适配器,相当于一个容器,代码如下:

public class HistoryAdapter extends ArrayAdapter<HistoryList> {    private int resourceId;    public HistoryAdapter(Context context, int textViewResourceId, List<HistoryList> objects){        super(context,textViewResourceId,objects);        resourceId=textViewResourceId;    }       //这个函数大致的意思就是将objects放入布局id为textViewResourceId的布局中,然后依次显示在context上。    @Override    public View getView(int position, View convertView, ViewGroup parent) {        HistoryList historyList = getItem(position); //获取当前项HistoryList的实例        View view = LayoutInflater.from(getContext()).inflate(resourceId, null);        TextView first=(TextView)view.findViewById(R.id.first111);        TextView op1=(TextView)view.findViewById(R.id.op1);        TextView second=(TextView)view.findViewById(R.id.second);        TextView op2=(TextView)view.findViewById(R.id.op2);        TextView third=(TextView)view.findViewById(R.id.third);        first.setText(historyList.get_first());        second.setText(historyList.get_second());        third.setText(historyList.get_third());        op1.setText(historyList.get_op1());        op2.setText(historyList.get_op2());        //调用HistoryList内的方法并添加到控件上        return view;    }}

适配器自定义完了,接下来就是自己添加数据到ArrayList内,数据可以是自定义的,也可以是从数据库中取出的,下面先贴上从数据库中获得数据并添加到ArrayList内的代码:

    public class lalala extends Activity {    private MyDatebaseHelper dbHelper;    private ArrayList<HistoryList> historyLists;    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.listview);        dbHelper = new MyDatebaseHelper(this);        Cursor data = dbHelper.getListContents();        historyLists = new ArrayList<>();        while (data.moveToNext()) {            String first = data.getString(data.getColumnIndex("first"));            String op1 = data.getString(data.getColumnIndex("op1"));            String second = data.getString(data.getColumnIndex("second"));            String op2 = data.getString(data.getColumnIndex("op2"));            String third = data.getString(data.getColumnIndex("third"));            HistoryList st = new HistoryList(first, op1, second, op2, third);            historyLists.add(st);        }        }}

上述代码实现了构造一个ArrayList容器,用于存放从Sqlite数据库中提取出来的数据。在这里调用了getListContents()方法,这是在MyDatabaseHelper内自定义的一个方法。这里直接贴上代码吧。

MyDatabaseHelper

public class MyDatebaseHelper extends SQLiteOpenHelper {    public static final String CREATE_BOOK= "create table book (" + "id integer primary key autoincrement, "            + "first text, "            + "op1 text, "            + "second text, "            + "op2 text, "            + "third text)";    private Context mContext;    public MyDatebaseHelper(Context context){        super(context,"HistoryList.db",null,1);    }    @Override    public void onCreate(SQLiteDatabase db){        db.execSQL(CREATE_BOOK);    }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    }    public Cursor getListContents(){        SQLiteDatabase db =this.getWritableDatabase();        Cursor data=db.rawQuery("SELECT *FROM "+ " book " ,null);        return data;    }}

要想使用数据库,首先要自己创建一个实例MyDatabaseHelper,在这个实例里自定义重构函数,当然也可以自己添加一些。

将数据显示在ListView控件上

 HistoryAdapter adapter=new HistoryAdapter(lalala.this,R.layout.layout,historyLists);        ListView listView=(ListView)findViewById(R.id.lv);        listView.setAdapter(adapter);

构造上述自创类HistoryAdpter实例,将存有数据的ArrayList容器作为HistoryAdapter(Context context, int textViewResourceId, List objects)函数里的objects,存放在textViewResource布局上,最后显示在ListView上。

大概就是这样了。。我也不知道有没有说清楚,因为理解ListView控件确实有一点点困难,我把我想说的说出来了。如果不懂,还欢迎评论。

另外有没有Android大神带带我啊!

原创粉丝点击