簡體   English   中英

從android上的SQLite數據庫填充ListView

[英]Populate ListView from SQLite database on android

我有這個數據庫,我需要用這些信息填充這個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;
    }

}

如何以簡單的方式填充此列表?

請幫忙。

ListView list = (ListView)findViewById(R.id.apptList);之后添加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);

PS你應該在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;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM