列表视图没有得到填充,getView()不获取调用 [英] Listview not getting populated, getView() isn't getting called

查看:307
本文介绍了列表视图没有得到填充,getView()不获取调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在让我的列表视图在一个简单的应用程序,我正在写展现出来的一些问题。出于某种原因, getView()我的适配器类的方法是没有得到调用。但是,如果当 getCount将()被称为我的适配器类是没有返回0,但实际上返回正确的值。我真的很困惑,为什么这是行不通的。

下面是我的活动:

 进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.ListView;公共类CardListActivity延伸活动{
保护的ListView LV;
保护INT nCards = 12;受保护的String [] = cardList新的String [nCards]公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.cardlist);    this.loadCardStrings();    LV =(ListView控件)findViewById(R.id.C​​ardList_ListView);
    EditorListAdapter适配器=新EditorListAdapter(这一点,this.cardList);
    lv.setAdapter(适配器);
}保护无效loadCardStrings(){
    cardList = getResources()getStringArray(R.array.card_list)。    Util.logD(使用创建的字符串数组:+ Integer.toString(car​​dList.length)+项目);
}
公共无效调用onStart(){
    super.onStart();
}
公共无效onResume(){
    super.onResume();
}
公共无效的onPause(){
    super.onPause();
}
公共无效的onStop(){
    super.onStop();}
公共无效的onDestroy(){
    super.onDestroy();
}
}

下面是活动中使用的布局:

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout机器人:ID =@ + ID / CardList_LinearLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    < TextView的机器人:ID =@ + ID /文
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:文字=你好/>
    < ListView的机器人:ID =@ + ID / CardList_ListView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT/>< / LinearLayout中>

最后,这里是我的适配器类:

 进口android.app.Activity;
进口android.graphics.Color;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.TextView;公共类EditorListAdapter延伸BaseAdapter
{
    活动背景;
    字符串名称[];    公共EditorListAdapter(活动的背景下,的String []标题){
        超();
        this.context =背景;
        this.names =称号;
    }    公众诠释的getCount(){
        Util.logD(EditorListAdapter.getCount()RET:+ Integer.toString(names.length));
        返回names.length;
    }    公共对象的getItem(INT位置){
        Util.logD(EditorListAdapter.getItem());
        返回null;
    }    众长getItemId(INT位置){
        Util.logD(EditorListAdapter.getItemId());
        返回0;
    }    公共查看getView(INT位置,查看convertView,父母的ViewGroup)
    {
        的TextView吨;
        如果(convertView == NULL)
            T =新的TextView(parent.getContext());
        其他
            T =(TextView中)convertView;        t.se​​tText(this.names [位置]);        convertView = T;        Util.logD(EditorListAdapter.getView()++ t.getText()的toString());        返回convertView;
    }    //每增加K-假面建议
    公众诠释getViewTypeCount(){
    返回1;
}
公众诠释getItemViewType(){
    返回0;
}
}


解决方案

试着改变的android:layout_height =FILL_PARENT为TextView中以 WRAP_CONTENT 来代替。我相信,从来没有得到显示列表视图,所以从来没有一个需要调用getView()

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

解决方案

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

这篇关于列表视图没有得到填充,getView()不获取调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆