草庐IT

Android BaseAdapter : convertView null on getView() re-entry

coder 2023-12-28 原文

我正在根据 http://bartinger.at/listview-with-sectionsseparators/ 中描述的技术构建一个包含部分的 ListView .但我想通过为非部分项目重用 convertView 来扩展它。但是,我发现每次输入 getView() 方法时 convertView 变量都为 null。有人可以解释为什么会这样吗?

ViewHolder holder;
final ListViewItem item = items.get(position);

if (item.isSection()) {
    Section section = (Section)item;

    convertView = inflater.inflate(R.layout.section, null);
    TextView title = (TextView) convertView.findViewById(R.id.section_title);
    title.setText(section.title);
} else {
    if (convertView == null) {
        Log.d("Adapter", "convertView was null");
    }

    Server server = (Server)item;

    convertView = inflater.inflate(R.layout.server_row, null);
    holder = new ViewHolder();
    holder.serverName = (TextView) convertView.findViewById(R.id.server_name);
    holder.serverStatusIcon = (ImageView)convertView.findViewById(R.id.server_status_icon);
    convertView.setTag(holder);

    holder.serverName.setText(server.name);
}

return convertView;

列表正在创建和显示,没有错误,并且包含部分和非部分项目都很好。

最佳答案

你是否正确实现

getItemViewType (int position) ?

参见Android的文档:

Returns

An integer representing the type of View. Two views should share the same type if one can be converted to the other in getView(int, View, ViewGroup). Note: Integers must be in the range 0 to getViewTypeCount() - 1. IGNORE_ITEM_VIEW_TYPE can also be returned.

所以也许 convertView 总是 null 因为适配器不知道哪些项目属于一起,所以它不知道哪些项目被回收...

试试这个:

@Override
public int getItemViewType(int position) {
    if (((MyItem)getItem(position)).isHeader()) {
        return 1;
    } else {
        return 0;
    }
}

@Override
public int getViewTypeCount() {
    return 2;
}

您在 getItemViewType 中返回的索引只是将 header 和非 header 分组在一起的标识符。

在这种情况下,您必须在模型项中实现方法“isHeader”(或类似方法)。

关于Android BaseAdapter : convertView null on getView() re-entry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11125286/

有关Android BaseAdapter : convertView null on getView() re-entry的更多相关文章

  1. ruby - 将 Gitlab 从 9.3.7 更新到 9.3.8 安装 re2 时出错 - 2

    我们在Ubuntu14.04和Gitlab9.3.7上运行,运行良好。我们正在尝试更新到Gitlabv9.3.8的最新安全补丁,但它给我们这个错误:Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension.currentdirectory:/home/git/gitlab/vendor/bundle/ruby/2.3.0/gems/re2-1.0.0/ext/re2/usr/local/bin/ruby-r./siteconf20170720-19622-15i0edf.rbextconf.rbcheckingformain(

  2. ruby-on-rails - 在没有 :total_entries to improve a lengthy query 的情况下使用 will_paginate - 2

    我有一个will_paginate的当前实现,它使用paginate_by_sql方法来构建要分页的集合。我们有一个针对total_entries的自定义查询,它非常复杂并且给我们的数据库带来了很大的负载。因此,我们想从分页中完全删除total_entries。换句话说,我们只需要一个“下一个-上一个”按钮,而不是“上一个1[2]345下一个”的典型分页显示。但我们需要了解一些事情。我们是否显示上一个链接?这当然只会发生在当前选择中显示的记录之前存在的记录我们是否显示下一个链接?如果显示集合中的最后一条记录,则不会显示此内容来自docsAqueryforcountingrowswill

  3. ruby - ruby 中#entries 和#to_a 的区别 - 2

    #entries之间的基本区别是什么?和#to_aEnumerable的方法ruby中的模块。两者似乎在Hash上返回相同的结果>>hash={"name"=>"foo","age"=>"23"}=>{"name"=>"foo","age"=>"23"}>>hash.to_a=>[["name","foo"],["age",23]]>>hash.entries=>[["name","foo"],["age",23]] 最佳答案 这是区别(查看#=>之后的输出):h={}h.method(:entries)#=>#h.method(:

  4. ruby - 使用 ruby​​ 应用程序时出现 remove_entry_secure 错误 - 2

    我正在尝试使用docsplit将PDF文件拆分为图像。但看来我的ruby​​安装有问题。我每次都会收到以下错误:/usr/lib/ruby/1.8/fileutils.rb:694:in`remove_entry_secure':parentdirectoryisworldwritable这是完整的命令行输出:$docsplitimagespdf-test.pdf/usr/lib/ruby/1.8/fileutils.rb:694:in`remove_entry_secure':parentdirectoryisworldwritable,FileUtils#remove_entry_

  5. ruby-on-rails - 无法写入未知属性 `scrapbook_entry_id' - 2

    正在尝试将数据添加到scrapbook_entries的连接表中,其中has_one:scrapbook和has_one:recipe。:recipe和:scrapbook已经存在。我正在尝试添加它们以将它们与scrapbook_entries表链接起来。form_for添加到scrapbook_entries表:scrapbook_entries_path(params[:id]))do|f|%>@recipe.id%>剪贴簿_条目_Controller:defcreate@recipe=Recipe.find(params[:scrapbook_entry][:recipe_id]

  6. ruby-on-rails - rails 上的 ruby : Creating a model entry with a belongs_to association - 2

    我正在尝试在我的数据库中为具有belongs_to关系的模型添加一个新条目。我有2个模型,工作和客户。很容易找到有关如何在这两者之间建立关联(使用has_many和belongs_to)的教程,但我似乎找不到任何实际使用关联的示例。在我的代码中,我正在尝试为第一个客户创建一个新工作。jobs模型有一个client_id属性,我知道我可以手动填充该属性,但必须有一些ruby​​约定才能轻松完成此操作。Job.create(:client_id=>1,:subject=>"Test",:description=>"Thisisatest")我可以很容易地将它放入我的代码中,但我觉得ruby

  7. ruby-on-rails - Spring 警告 : You're using Rubygems 2. 0.3。至少升级到 Rubygems 2.1.0 - 2

    我收到错误:警告:您正在将Rubygems2.0.3与Spring一起使用。尝试运行Rails控制台时至少升级到Rubygems2.1.0。我该如何更新它? 最佳答案 尝试运行gemupdate--system来更新Rubygems本身。 关于ruby-on-rails-Spring警告:You'reusingRubygems2.0.3。至少升级到Rubygems2.1.0,我们在StackOverflow上找到一个类似的问题: https://stackov

  8. ruby - `Dir.entries` 中的排序顺序 - 2

    Dir.entries返回结果是否有固定/默认的排序顺序?我根据经验知道前两个条目是"."和"..". 最佳答案 根据Ruby语言文档,Dir.entries()不保证所列文件的任何特定顺序,因此如果您需要某种顺序,最好自己明确执行。例如,如果您需要按文件修改时间排序(从旧到新),您可以执行以下操作:Dir.entries('.').sort_by{|x|File.mtime(x)} 关于ruby-`Dir.entries`中的排序顺序,我们在StackOverflow上找到一个类似的问

  9. es报Unexpected character (‘ï‘ (code 239)): was expecting comma to separate Object entries解决方法 - 2

    【现象】执行es命令时,报如下错误:{ "error":{  "root_cause":[   {    "type":"parse_exception",    "reason":"Failedtoparsecontenttomap"   }  ],  "type":"parse_exception",  "reason":"Failedtoparsecontenttomap",  "caused_by":{   "type":"json_parse_exception",   "reason":"Unexpectedcharacter('ï'(code239)):wasexpectingc

  10. javascript - Vue 3 CLI - 如何为 Object.entries 添加 babel polyfill - 2

    我有一个依赖项(vue2-google-maps),它导致我的Vue应用程序在旧版浏览器中出现问题,并引发Object.entries错误。阅读VueCLIDocsonpolyfills,我看到它提到尝试在babel.config.js中加载polyfill。我的babel.config.js:module.exports={presets:[['@vue/app',{polyfills:['es6.object']}]]}抛出错误:Modulebuildfailed(from./node_modules/babel-loader/lib/index.js):TypeError:[BA

随机推荐