从android上的SQLite数据库填充ListView

时间:2014-03-22 22:01:50

标签: android xml sqlite listview android-listview

我有这个数据库,我需要用信息填充这个ListView

我的DBHandler类上已有一个方法:

 public List<Appointment> getAppointments(){
            List<Appointment> appointmentList = new ArrayList<Appointment>();
            String selectQuery = "SELECT * FROM " + TABLE_APP;//query to search appointment by title
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);
            if(cursor.moveToFirst()){
                do{
                    Appointment appointment = new Appointment();
                    appointment.setID(Integer.parseInt(cursor.getString(0)));
                    appointment.setDate(cursor.getString(1));
                    appointment.setTitle(cursor.getString(2));
                    appointment.setTime(cursor.getString(3));
                    appointment.setDetails(cursor.getString(4));

                    appointmentList.add(appointment);
                } while(cursor.moveToNext());
            }

            return appointmentList;
        }

现在我需要填充DeleteAppointment类的列表:

   package com.example.calendar;


import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class DeleteAppointment extends Activity implements OnClickListener{

    Appointment app;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.delete);
        Button delete = (Button)findViewById(R.id.btn_delete);
        Button deleteAll = (Button)findViewById(R.id.btn_deleteAll);
        ListView list = (ListView)findViewById(R.id.apptList);
        DBHandler db = new DBHandler(this);
        String[] columns = new String[]{app._date, app._title, app._time, app._details};
        int[] viewIDs = new int[]{R.id.date, R.id.name, R.id.time, R.id.details};
        Cursor cursor = (Cursor) db.getAppointments();
        SimpleCursorAdapter adapter;
        adapter = new SimpleCursorAdapter(getBaseContext(), R.layout.item, cursor, columns, viewIDs, 0);
        list.setAdapter(adapter);
        }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.btn_delete:
            finish();
        break;
        }

    }

}

布局的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ListView android:id="@+id/apptList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></ListView>

        <TableLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TableRow>

                <Button
                    android:id="@+id/btn_delete"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/deleteDialog"/>

                <Button
                    android:id="@+id/btn_deleteAll"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/deleteAll"/>

            </TableRow>

        </TableLayout>
    </LinearLayout>


</ScrollView>

项目布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

预约课程:

package com.example.calendar;

import java.sql.Date;

public class Appointment {

    //variables(that are the attributes of the database table)
    int _id;
    String _title;
    String _time;
    String _details;
    String _date;

    //empty constructor to add for the update method in the DBHandler
    public Appointment(){

    }

    public Appointment(int id, String date, String title, String time, String details){

        this._id = id;
        this._date = date;
        this._title = title;
        this._time = time;
        this._details = details;

    }

    public Appointment(String date, String title, String time, String details){

        this._date = date;
        this._title = title;
        this._time = time;
        this._details = details;

    }

    //----------GET/SET METHODS BELOW-----------

    //--------ID---------

    public int getID(){
        return this._id;
    }

    public void setID(int id){
        this._id = id;
    }

    //-------DATE-------

    public String getDate(){
        return this._date;
    }

    public void setDate(String date){
        this._date = date;
    }

    //-----TITLE---------

    public String getTitle(){
        return this._title;
    }

    public void setTitle(String title){
        this._title = title;
    }

    //------TIME-------

    public String getTime(){
        return this._time;
    }

    public void setTime(String time){
        this._time = time;
    }

    //------DETAILS--------

    public String getDetails(){
        return this._details;
    }

    public void setDetails(String details){
        this._details = details;
    }

}

如何以简单的方式填充此列表?

请帮忙。

1 个答案:

答案 0 :(得分:3)

ListView list = (ListView)findViewById(R.id.apptList);

之后添加此内容
ArrayAdapter<Appointment> mArrayAdapter = new ArrayAdapter<Appointment>(this, android.R.layout.simple_list_item_1, android.R.id.text1, getAppointments());
list.setAdapter(mArrayAdapter);

P.S。您应该覆盖Appointment中的方法toString()来控制列表视图中显示的内容。

添加:

@Override
public String toString() {
    return _title;
}

到您的预约课程,如下所示:

public class Appointment {

    //variables(that are the attributes of the database table)
    int _id;
    String _title;
    String _time;
    String _details;
    String _date;

    //empty constructor to add for the update method in the DBHandler
    public Appointment(){

    }

    public Appointment(int id, String date, String title, String time, String details){

        this._id = id;
        this._date = date;
        this._title = title;
        this._time = time;
        this._details = details;

    }

    public Appointment(String date, String title, String time, String details){

        this._date = date;
        this._title = title;
        this._time = time;
        this._details = details;

    }

    //----------GET/SET METHODS BELOW-----------

    //--------ID---------

    public int getID(){
        return this._id;
    }

    public void setID(int id){
        this._id = id;
    }

    //-------DATE-------

    public String getDate(){
        return this._date;
    }

    public void setDate(String date){
        this._date = date;
    }

    //-----TITLE---------

    public String getTitle(){
        return this._title;
    }

    public void setTitle(String title){
        this._title = title;
    }

    //------TIME-------

    public String getTime(){
        return this._time;
    }

    public void setTime(String time){
        this._time = time;
    }

    //------DETAILS--------

    public String getDetails(){
        return this._details;
    }

    public void setDetails(String details){
        this._details = details;
    }

    @Override
    public String toString() {
        return _title;
    }

}