1

For some reasons getView of my customized ArrayAdapter isn't called and android displays an empty list. I've already inserted cursor.getCount() in my datasource class to make sure my query isn't empty.

Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate called");

    setContentView(R.layout.conference_list_layout);

    Log.d(TAG, "create ConferenceDataSource");
    datasource = new ConferenceDataSource(this);

    Log.d(TAG, "open ConferenceDataSource");
    datasource.open();

    Log.d(TAG, "getAllUpcomingConferences");
    List<Conference> conferences = datasource.getAllUpcomingConferences();

    Log.d(TAG, "set ConferenceArrayAdapter");
    setListAdapter(new ConferenceArrayAdapter(this, conferences));
}

Datasource:

public List<Conference> getAllUpcomingConferences() {
    Log.d(TAG, "getAllUpcomingConferences called");

    List<Conference> conferences = new ArrayList<Conference>();

    Cursor cursor = database.query(
            DatabaseConstants.TABLE_CONFERENCES, 
            fields, 
            DatabaseConstants.CONFERENCE_START + ">" + (Calendar.getInstance()).getTimeInMillis(), 
            null, 
            null, 
            null, 
            null);

    cursor.moveToFirst();

    while(!cursor.isAfterLast()) {
        Conference conference = cursorToConference(cursor);
        conferences.add(conference);
        cursor.moveToNext();
    }

    cursor.close();

    return conferences;
}

private Conference cursorToConference(Cursor cursor) {
    Log.d(TAG, "cursorToConference called");

    Conference conference = new Conference();

    conference.setId(
            cursor.getLong(cursor.getColumnIndex(DatabaseConstants.CONFERENCE_ID)));

    conference.setStart(
            cursor.getLong(cursor.getColumnIndex(DatabaseConstants.CONFERENCE_START)));

    conference.setEnd(
            cursor.getLong(cursor.getColumnIndex(DatabaseConstants.CONFERENCE_END)));

    conference.setTopic(
            cursor.getString(cursor.getColumnIndex(DatabaseConstants.CONFERENCE_TOPIC)));

    conference.setCreator(
            cursor.getLong(cursor.getColumnIndex(DatabaseConstants.CONFERENCE_CREATOR)));

    return conference;
}

ArrayAdapter:

public ConferenceArrayAdapter(Context context, List<Conference> conferences) {
    super(context, R.layout.conference_list_item);
    Log.d(TAG, "constructor called");
    this.context = context;
    this.conferences = conferences;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Log.d(TAG, "getView called");

    LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view = inflator.inflate(R.layout.conference_list_item, parent, false);

    Date conferenceDate = new Date(conferences.get(position).getStart());

    TextView dayOfWeek = (TextView) view.findViewById(R.id.conference_list_dayofweek);

    dayOfWeek.setText(new SimpleDateFormat("E").format(conferenceDate));

    TextView date = (TextView) view.findViewById(R.id.conference_list_date);

    date.setText(new SimpleDateFormat("dd.MM.yyyy").format(conferenceDate));

    TextView time = (TextView) view.findViewById(R.id.conference_list_time);

    time.setText(new SimpleDateFormat("HH:mm").format(new Date(conferences.get(position).getStart())) + 
            new SimpleDateFormat("HH:mm").format(new Date(conferences.get(position).getEnd())));

    return view;
}

conference_list_layout.xml

<include layout="@layout/actionbar_layout" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

conference_list_item.xml

<TextView
    android:id="@+id/conference_list_dayofweek"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

<TextView
    android:id="@+id/conference_list_date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/conference_list_dayofweek" />

<TextView
    android:id="@+id/conference_list_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/conference_list_date" />

Could it be, that the error is in the xml file. I'm using several customized ArrayAdapters and they all work fine and look nearly the same. I've read something about using match_parent instead of wrap_content but it didn't work out.

2 Answers 2

8

In your custom adapter(your probably extending ArrayAdapter) implement the getCount method and return the list of Conferences size:

public int getCount() {
   return conferences.size();
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot this really worked out ... omg. Why is it working with my other ArrayAdapters without overriding the getCount function?
@sebbl.sche In the other adapters you probably use other super constructors from the parent class ArrayAdapter(that also take a list/array of objects representing the data, from that list the adapter knows what to do in the getCount method). In your adapter from the question you use the constructor that takes only the Context and a resource id so this adapter doesn't have any data set(so the getCount method return 0, the adapter is empty, the getView isn't called). Check the docs for the ArrayAdapter class and check the other constructors.
0

You need to extends ArrayAdapter for your array adapter class - otherwise it does not know the method to be called

1 Comment

I extended it. My class definition looks like this. public class ConferenceArrayAdapter extends ArrayAdapter<Conference>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.