ListView and Sqlite Database

1 篇文章 0 订阅
1 篇文章 0 订阅
距离上一次更新博客距离九天,这一次,写了个将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大神带带我啊!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值