Problem to the popular a Listview

Asked

Viewed 313 times

1

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

Here is 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 popular the ListView:

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

And finally, that’s the function that reads events from inside the comic:

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

  • Have you tried checking the size of the Events list to see if they are there? Could be an SQL error.

  • I think the problem is not Sqlite error because it does not return any error information in logcat. In this case, I noticed that my function to return the user ID is returning me 0 (in the case of the test user), instead of 1, as it should be the first BD ID. Could you tell me why this happens? It still doesn’t make sense to me.

  • new String[] { "id", email } no quotes are missing in the email?

  • No, the email is a String passed as a function variable: public int getUserId(String email)

  • Makes a select * See what comes back.

  • Making a select * returned the correct value, but it’s not working yet. It correctly registers the event in the BD (or better, does not generate error message when doing the same), a Singleton is receiving the value of the user’s ID, but at the time of recovering this data or nothing is returned or there is something wrong with my adapter.

  • Makes a Log.e("Singleton", SingletonUser.getInstance().getId()+""); at the beginning of readEvents() and see what happens.

Show 3 more comments

1 answer

1


After a lot of work, I managed to solve it in the following way:

1.First I changed the function query getUserId of:

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

for:

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

Only then I was able to recover the user ID correctly.

2.I moved the function loadEvents() into the method onActivityCreated:

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

By doing so, I made sure that the list would only be populated after the screen content had been created (without this it gave a force close)

3.I corrected the declaration of EventListAdapter, before 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 that I got Listview popular correctly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.