【问题标题】:Populate ListView from SQLite database on android从 Android 上的 SQLite 数据库填充 ListView
【发布时间】:2014-03-22 22:01:49
【问题描述】:

我有这个数据库,我需要用信息填充这个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;
    }

}

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

请帮忙。

【问题讨论】:

  • 简单的方法?使用 SimpleCursorAdapter
  • 和?它是否有效?
  • @pskink 我很接近,但不是很接近
  • 没有多大帮助,怎么了?
  • 给出下面的答案,我需要填充列表,但该行必须在一个大字符串中包含所有属性。我该怎么做?

标签: android xml sqlite listview android-listview


【解决方案1】:

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);

附:您应该重写 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;
    }

}

【讨论】:

  • 它不工作。它不显示约会,而是显示 com.example.calendar.Appointment@417f1980
  • 你应该重写 Appointment 中的 toString() 方法来控制列表视图中显示的内容,20 分钟前写的 ;)
  • 覆盖方法 toString() 还是公共列表本身?
  • toString 返回的内容都会显示在列表视图中,所以如果你想显示名称+时间,那么一定要返回名称+“”+时间;
  • 我认为您将需要一个更加自定义的视图,将来您可以搜索“listview example”或单击此处vogella.com/tutorials/AndroidListView/article.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-09
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多