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?