1

I have this database and I need to populate this ListView with the information.

I already have a method on my DBHandler class which is:

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

And now I need to populate the list on my DeleteAppointment class:

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

    }

}

And the xml file for the layout:

<?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>

The item layout:

<?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>

And the Appointment class:

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

}

How can I populate this list in a simple way?

Please help.

10
  • simple way? use SimpleCursorAdapter Commented Mar 22, 2014 at 22:06
  • and? is it working or not? Commented Mar 23, 2014 at 8:36
  • @pskink I'm close but not quite there Commented Mar 23, 2014 at 9:04
  • it doesnt help much, whats wrong? Commented Mar 23, 2014 at 9:31
  • given the answer below, I need to populate the list but the row has to have all the attri ute in one big string. How do I do that? Commented Mar 23, 2014 at 9:38

1 Answer 1

3

Add this after 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. You should override the method toString() in Appointment to control what get's displayed in the list view.

Add:

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

to your appointment class to make it like this:

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

}
Sign up to request clarification or add additional context in comments.

12 Comments

it's not working. Instead of showing the appointments, it shows com.example.calendar.Appointment@417f1980
You should override the method toString() in Appointment to control what get's displayed in the list view, Wrote that 20 minutes ago ;)
override the method toString() or the public list itself?
What ever toString returns will be displayed in the list view, so if you want to display name + time then by all means return name + " " + time;
I recon you will need a more customized view, in the future for that you can search for "listview example" or click here vogella.com/tutorials/AndroidListView/article.html
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.