【问题标题】:Listview not getting populated, getView() isn't getting calledListview 没有被填充,getView() 没有被调用
【发布时间】:2011-11-15 13:09:33
【问题描述】:

我在让我的列表视图显示在我正在编写的一个简单应用程序中时遇到了一些问题。由于某种原因,我的适配器类的getView() 方法没有被调用。但是,当在我的适配器类中调用 getCount() 时,它并没有返回 0,而是实际上返回了正确的值。我真的很困惑为什么这不起作用。

这是我的活动:

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();
}   
}

这是活动使用的布局:

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

最后是我的适配器类:

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;
}
}

【问题讨论】:

  • 尝试实现getViewTypeCount并使其返回1;然后 getItemViewType 应该返回 0。
  • 我加了但是没有效果...
  • 您是否尝试过实现所有适配器的方法?如果您的代码有任何根本性的错误,我看不到它。
  • 甚至更好,而不是扩展 BaseAdapter 扩展 ArrayAdapter&lt; String &gt; 并仅覆盖 getView
  • 好吧,我也试过了,但是 getView 方法仍然没有被调用.... :-(

标签: android android-listview android-adapter


【解决方案1】:

尝试将 textview 的 android:layout_height="fill_parent" 改为 wrap_content。我相信列表视图永远不会显示,所以永远不需要调用 getView()

【讨论】:

  • 啊,是的,解决了我的问题(大约 1 年后)。我喜欢stackoverflow。
  • 我遇到了同样的问题,我检查了 getCount 它总是返回列表的大小。我的列表行布局也包含 fill_parent。但是没有调用视图。相同的列表行布局适用于其他适配器
  • 真是个新手错误。感谢您的回答。
【解决方案2】:

我知道这个问题已经有了答案。

但是,我的根本原因和原因与原始问题的症状相似(未调用 getView()),因此我认为在此处记录对于具有不同根本原因的其他人是有益的。

http://hissain.in/wordpress/?p=659给出

“有时 notifyDataSetChanged() 似乎对你不起作用(因此 getView() 不会被调用)。 原因可能是您的适配器丢失了对您列表的引用。当您第一次初始化适配器时,它会引用您的数组列表并传递给它的超类。但是,如果您重新初始化现有的数组列表,它会丢失引用,因此会丢失与适配器的通信通道”

因此永远不要重新初始化您的列表,而是在列表上使用 clear()。

list.clear(); // Right Thing to Do
list = new List<T>(); // Wrong Thing to Do, will lose reference in the adapter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    相关资源
    最近更新 更多