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.
– Jorge B.
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.
– Renan Lazarotto
new String[] { "id", email }
no quotes are missing in the email?– Jorge B.
No, the
email
is aString
passed as a function variable:public int getUserId(String email)
– Renan Lazarotto
Makes a
select *
See what comes back.– Jorge B.
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), aSingleton
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.– Renan Lazarotto
Makes a
Log.e("Singleton", SingletonUser.getInstance().getId()+"");
at the beginning ofreadEvents()
and see what happens.– Jorge B.
let’s go continue this discussão in chat
– Renan Lazarotto