16

我在让我的列表视图显示在我正在编写的一个简单应用程序中时遇到了一些问题。由于某种原因getView(),我的适配器类的方法没有被调用。但是,当getCount()在我的适配器类中调用 when 时,它没有返回 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;
}
}
4

2 回答 2

30

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

于 2011-09-12T18:45:36.930 回答
4

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

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

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

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

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

list.clear(); // Right Thing to Do
list = new List<T>(); // Wrong Thing to Do, will lose reference in the adapter
于 2014-04-09T04:40:46.420 回答