Listview not getting populated, getView() isn't getting called
Asked Answered
F

2

16

I'm having a few problems getting my listview to show up in a simple application i'm writing. For some reason the getView() method of my adapter class isn't getting called. However, when when getCount() is called in my adapter class it isn't returning 0 but is in fact returning the proper value. I'm really confused as to why this isn't working.

Here is my Activity:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class CardListActivity extends Activity {


protected ListView lv;
protected int nCards = 12;

protected String[] cardList = new String[nCards];

public void onCreate(Bundle savedInstanceState) {       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cardlist);

    this.loadCardStrings();

    lv = (ListView) findViewById(R.id.CardList_ListView);
    EditorListAdapter adapter = new EditorListAdapter(this, this.cardList);
    lv.setAdapter(adapter); 
}

protected void loadCardStrings(){
    cardList = getResources().getStringArray(R.array.card_list);

    Util.logD("Created the string arrays with:" + Integer.toString(cardList.length) + " items.");
}   
public void onStart(){
    super.onStart();
}
public void onResume(){
    super.onResume();
}
public void onPause(){
    super.onPause();
}
public void onStop(){
    super.onStop();

}
public void onDestroy(){
    super.onDestroy();
}   
}

Here is the layout used by the activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/CardList_LinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Hello"/>
    <ListView android:id="@+id/CardList_ListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

Finally here is my adapter class:

import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class EditorListAdapter extends BaseAdapter
{
    Activity context;
    String names[];

    public EditorListAdapter(Activity context, String[] title) {
        super();
        this.context = context;
        this.names = title;
    }

    public int getCount() {
        Util.logD("EditorListAdapter.getCount() ret:" + Integer.toString(names.length));
        return names.length;
    }

    public Object getItem(int position) {
        Util.logD("EditorListAdapter.getItem()");
        return null;
    }

    public long getItemId(int position) {
        Util.logD("EditorListAdapter.getItemId()");
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        TextView t;
        if (convertView == null)
            t = new TextView(parent.getContext());
        else
            t = (TextView) convertView;

        t.setText(this.names[position]);

        convertView = t;

        Util.logD("EditorListAdapter.getView() + " + t.getText().toString());

        return convertView;
    }

    // Added per K-Ballo suggestion
    public int getViewTypeCount(){
    return 1;
}
public int getItemViewType(){
    return 0;
}
}
Focal answered 12/9, 2011 at 17:45 Comment(5)
Try implementing getViewTypeCount and make it return 1; and then getItemViewType should return 0.Fetter
I added them but there was no effect...Focal
Have you tried implementing all of the Adapter's methods? If there is anything fundamentally wrong with your code, I fail to see it.Fetter
Even better, instead of extending a BaseAdapter extend an ArrayAdapter< String > and override only getView.Fetter
Ok I tried that too, but the getView method is still not getting called.... :-(Focal
R
30

Try changing the android:layout_height="fill_parent" for the textview to wrap_content instead. I believe the listview is never getting displayed, so there is never a need to call getView()

Ringtailed answered 12/9, 2011 at 18:45 Comment(3)
Aw yeah, solved my problem (almost 1 year later). I love stackoverflow.Coronado
i am getting same issue i have checked getCount it always return the size for the list. my list row layout contains fill_parent also. But get view not called. Same list row layout works in other adapterWeatherboarding
What a rookie mistake. Thanks for the answer.Horme
S
4

I know there is already an answer to this question.

However, I am having a different root cause and reason with similar symptom to the original question (getView() isn't called), hence I think it is beneficial to document at here for other people having different root cause.

As given by http://hissain.in/wordpress/?p=659

“Sometimes it seems notifyDataSetChanged() won’t work for you (and hence getView() won't get called). The reason could be your adapter loses reference to your list. When you first initialize the Adapter it takes a reference of your array list and pass to its superclass. But if you reinitialize your existing array list it loses the reference hence the communication channel with Adapter”

Hence never reinitialize your list, but instead use clear() on the list.

list.clear(); // Right Thing to Do
list = new List<T>(); // Wrong Thing to Do, will lose reference in the adapter
Skantze answered 9/4, 2014 at 4:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.