草庐IT

android - Listview 没有被填充,getView() 没有被调用

coder 2023-06-10 原文

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

这是我的 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();
}   
}

这是 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>

最后是我的适配器类:

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

最佳答案

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

关于android - Listview 没有被填充,getView() 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7391797/

有关android - Listview 没有被填充,getView() 没有被调用的更多相关文章

  1. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  2. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  3. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  4. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  5. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  6. ruby - 匹配大写字母并用后续字母填充,直到一定的字符串长度 - 2

    我有一个驼峰式字符串,例如:JustAString。我想按照以下规则形成长度为4的字符串:抓取所有大写字母;如果超过4个大写字母,只保留前4个;如果少于4个大写字母,则将最后大写字母后的字母大写并添加字母,直到长度变为4。以下是可能发生的3种情况:ThisIsMyString将产生TIMS(大写字母);ThisIsOneVeryLongString将产生TIOV(前4个大写字母);MyString将生成MSTR(大写字母+tr大写)。我设法用这个片段解决了前两种情况:str.scan(/[A-Z]/).first(4).join但是,我不太确定如何最好地修改上面的代码片段以处理最后一种

  7. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  8. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  9. ruby - 调用其他方法的 TDD 方法的正确方法 - 2

    我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent

  10. 没有类的 Ruby 方法? - 2

    大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow

随机推荐