Listview 未填充,getView() 未调用
我在让列表视图显示在我正在编写的简单应用程序中时遇到一些问题。由于某种原因,我的适配器类的 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;
}
}
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将文本视图的
android:layout_height="fill_parent"
更改为wrap_content
。我相信列表视图永远不会显示,所以永远不需要调用 getView()Try changing the
android:layout_height="fill_parent"
for the textview towrap_content
instead. I believe the listview is never getting displayed, so there is never a need to call getView()我知道这个问题已经有答案了。
但是,我有不同的根本原因和原因,其症状与原始问题类似(未调用 getView() ),因此我认为在这里记录对于具有不同根本原因的其他人是有益的。
正如 http://hissain.in/wordpress/?p=659 所给出的
“有时它似乎notifyDataSetChanged()对你不起作用(因此getView()不会被调用)。 原因可能是您的适配器丢失了对列表的引用。当您第一次初始化适配器时,它会获取数组列表的引用并传递给其超类。但是,如果您重新初始化现有的数组列表,它将丢失引用,从而失去与适配器的通信通道”
因此,永远不要重新初始化您的列表,而是在列表上使用clear()。
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.