Usman
Usman

Reputation: 51

How to display all records of a table in list View?

Here is my query class which has method getuserEvents() and i want to show all the records of my table in another class but the problem which i am facing it gives me only the first record.

public ArrayList<String> getUserEvents() {
    ArrayList<String> EventsList = new ArrayList<String>();
    Cursor c = this.database.rawQuery("select FEventId, FEventName, FEventDescription, FEventStatus, FEventReligion, FromDate, ToDate from tblApEvents", new String[]{} );
    if (c.moveToFirst()) {
        do {
            EventsList.add(c.getString(0));
            EventsList.add(c.getString(1));
            EventsList.add(c.getString(2));
            EventsList.add(c.getString(3));
            EventsList.add(c.getString(4));
            EventsList.add(c.getString(5));
            EventsList.add(c.getString(6));
        } while (c.moveToNext());
    }
    return EventsList;
}

    QueryClass qc = new QueryClass(getApplication());
    qc.open();
    EventsList=qc.getUserEvents();

    if (!EventsList.equals("")) {

        id.add(EventsList.get(0));
        EventName.add(EventsList.get(1));
        EDescription.add(EventsList.get(2));
        EStatus.add(EventsList.get(3));
        EReligion.add(EventsList.get(4));
        EventTDate.add(EventsList.get(7));
        qc.close();

    }

Upvotes: 0

Views: 104

Answers (1)

Rustam
Rustam

Reputation: 6515

your method should look like this:

 public ArrayList<Event> getUserEvents() {
        ArrayList<Event> EventsList = new ArrayList<>();
        Cursor c = this.database.rawQuery("select FEventId, FEventName, FEventDescription, FEventStatus, FEventReligion, FromDate, ToDate from tblApEvents", new String[]{} );
        if (c.moveToFirst()) {
            do {
                EventsList.add(new Event(c.getString(0),c.getString(1),c.getString(2),c.getString(3),c.getString(4),c.getString(5),c.getString(6)));
            } while (c.moveToNext());
        }
        return EventsList;
    }

your Event class:

class Event{

private String FEventId;
private String FEventName;
    // other members
    //parameterized constructor
    // setter and getter

}

finally create your custom adapter and populate the Event data in your listview

Upvotes: 1

Related Questions