Problem when populating a ListView

1

I need to popular a ListView that is using a custom adapter in my application but nothing happens, the list simply goes blank.

Here's the adapter:

package adapters;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import br.bravosix.compromissos.R;
import classes.Event;

public class EventListAdapter extends ArrayAdapter<String> {

    private final Context context;
    private final List<Event> events;

    public EventListAdapter(Context context, List<Event> events) {
        super(context, R.layout.event_layout);
        this.context = context;
        this.events = events;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.event_layout, parent, false);

        TextView titleText = (TextView) rowView.findViewById(R.id.event_title);
        TextView descText = (TextView) rowView.findViewById(R.id.event_date);

        Event event = new Event();
        event = events.get(position);

        titleText.setText(event.toString());
        descText.setText(event.getDescription());
        return rowView;
    }
}

This is the function used to populate ListView :

public void loadEvents() {
    Database db = new Database(getActivity());
    EventListAdapter listEvents = new EventListAdapter(getActivity(),
            db.readEvents());
    setListAdapter(listEvents);
}

And finally, this is the function that reads the events from within the DB:

public List<Event> readEvents() {

    List<Event> events = new LinkedList<Event>();
    SQLiteDatabase db = this.getWritableDatabase();

    String query = "SELECT * FROM " + table_event + " WHERE user_id="
            + SingletonUser.getInstance().getId()
            + " ORDER BY name COLLATE NOCASE ASC";

    Cursor cursor = db.rawQuery(query, null);

    Event event = null;

    if (cursor.moveToFirst()) {
        do {
            event = new Event();
            event.setId(Integer.parseInt(cursor.getString(0)));
            event.setUserId(Integer.parseInt(cursor.getString(1)));
            event.setName(cursor.getString(2));
            event.setPlace(cursor.getString(3));
            event.setDate(cursor.getString(4));
            event.setContact(cursor.getString(5));
            event.setNotes(cursor.getString(6));

            events.add(event);
        } while (cursor.moveToNext());
    }
    return events;
}

I'm setting the user ID within a Singleton so that it can be accessible from any part of the code using this function:

public int getUserId(String email) {
    SQLiteDatabase db = this.getWritableDatabase();

    Cursor cursor = db.rawQuery("SELECT ? FROM " + table_user
            + " WHERE email=?", new String[] { "id", email });

    cursor.moveToFirst();

    int count = cursor.getCount();

    if (count < 0) {
        return -1;

    } else {
        return cursor.getInt(0);
    }
}

Where am I going wrong?

    
asked by anonymous 06.05.2014 / 06:00

1 answer

1

After a lot of work, I was able to resolve it as follows:

1. First I changed the query of the function getUserId of:

Cursor cursor = db.rawQuery("SELECT ? FROM " + table_user
        + " WHERE email=?", new String[] { "id", email });

To:

Cursor cursor = db.rawQuery("SELECT * FROM " + table_user
                + " WHERE email=?", new String[] { email });

This is the only way I have been able to retrieve the user ID correctly.

2.Move the loadEvents() function into the onActivityCreated method:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    loadEvents();
}

Doing this, I ensured that the list would only be populated after the content of the screen was created (without this gave a force close)

3. Fixed the declaration of EventListAdapter , before it was:

public class EventListAdapter extends ArrayAdapter<String> {

    private final Context context;
    private final List<Event> events;

    public EventListAdapter(Context context, List<Event> events) {
        super(context, R.layout.event_layout);
        this.context = context;
        this.events = events;
    }

and became:

public class EventListAdapter extends ArrayAdapter<Event> {

    private final Context context;
    private final List<Event> events;

    public EventListAdapter(Context context, List<Event> events) {
        super(context, R.layout.event_layout, events);
        this.context = context;
        this.events = events;
    }

After this I got the ListView popular.

    
06.05.2014 / 18:28