- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在我的自定义 ListAdapter 中,第一次调用 GetView() 时,convertView 作为 NULL 传入,但第二次作为第一次创建的 View 传入。我的 ListView 有 4 行,所有 4 行同时出现在屏幕上。从文档来看,似乎 convertView 应该是一个已经创建并且现在已经滚出屏幕的 View 。我希望 convertView 全部 4 次都为 null,以便它会创建/膨胀 4 个单独的 View 。我应该在第一次调用 getView 后有一个 convertView 吗?谢谢。
在 OnCreate() 中:
Cursor questions = db.loadQuestions(b.getLong("categoryId"), inputLanguage.getLanguageId(), outputLanguage.getLanguageId());
startManagingCursor(questions);
ListAdapter adapter = new QuestionsListAdapter(this, questions);
ListView list = (ListView)findViewById(R.id.list1);
setListAdapter(adapter);
适配器类
private class QuestionsListAdapter extends BaseAdapter implements ListAdapter{
private Cursor c;
private Context context;
public QuestionsListAdapter(Context context, Cursor c) {
this.c = c;
this.context = context;
}
public Object getItem(int position) {
c.moveToPosition(position);
return new Question(c);
}
public long getItemId(int position) {
c.moveToPosition(position);
return new Question(c).get_id();
}
@Override
public int getItemViewType(int position) {
Question currentQuestion = (Question)this.getItem(position);
if (currentQuestion.getType().equalsIgnoreCase("text"))
return 0;
else if (currentQuestion.getType().equalsIgnoreCase("range"))
return 0;
else if (currentQuestion.getType().equalsIgnoreCase("yesNo"))
return 2;
else if (currentQuestion.getType().equalsIgnoreCase("picker"))
return 0;
else if (currentQuestion.getType().equalsIgnoreCase("command"))
return 0;
else if (currentQuestion.getType().equalsIgnoreCase("datePicker"))
return 0;
else if (currentQuestion.getType().equalsIgnoreCase("diagram"))
return 0;
else
return -1;
}
@Override
public int getViewTypeCount() {
return 7;
}
public int getCount() {
return c.getCount();
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
Question currentQuestion = (Question)this.getItem(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.question_row_text, null);
}
//setup cell
return convertView;
}
}
最佳答案
如果我没记错的话,GetView
将为要显示的每一行调用两次。我认为第一组调用是为了布局目的而完成的,第二组返回将实际显示的 View 。第二组调用应该返回与第一组调用相同的 View ,这是有道理的。
在任何情况下,您的代码都不应该关心它是被调用用于布局还是显示。在几乎所有情况下,如果 convertView
不为空,那么通常应该返回 convertView
;否则,您应该返回一个新 View 。
关于android - ConvertView 作为仍在屏幕上的 View 传入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6272193/
上下文 我想要一个包含 3 种截然不同的列表项布局的列表,因此我使我的适配器根据要显示的项目类型创建适当的 View 。 例如我想列出一些图像、文本和数字,每个都有一些标题。我知道在 public V
文章中Multithreading For Performance来自Android Developer Blog,在Adapter.getItem()中使用convertView来显示通过HttpR
我正在尝试显示具有不同类型行的ListView。我有一个由 Artist、Label、Release 等继承的 Result 抽象类。我从 JSON 响应中获取结果。解析正常,我的工厂成功创建了一些
我正在努力思考 getView(),我认为 Android 中没有任何主题会在 StackOverflow 和其他地方引起更多的困惑和问题。每个人都想知道为什么它被调用了这么多次,以什么顺序调用或者根
我正在使用 fragment 创建一个 ListView,其中填充了 CheckBoxes。我尽力根据其他问题找到解决方案,但我似乎无法修复它。当我不使用以下命令运行脚本时: convertView.
这是我的getView()。 我显然在这里做错了什么,因为我列表的第一项总是没有显示图片。 这里的问题出在 convertview 上,因为如果我不回收它,就没有问题。 请问我做错了什么?? @Ove
我正在制作一个使用 ListView 和列表适配器的应用程序。它工作正常,但我在 getgroupview 方法中发现了内存泄漏。在我每次都膨胀 xml 文件并导致泄漏之前。 groupRow =
我有一个 ListView,其中包含一个 ImageView 和一个 TextView。我将 ArrayAdapter 子类化,以便我可以通过子类化的 AsyncTask 从 Internet 加载图
我正在实现自定义适配器,它根据这个(非常有用的)教程在 listview 中处理多种类型的行:http://logc.at/2011/10/10/handling-listviews-with-mul
我正在使用带有 ImageAdapter 的 Gallery 加载它,并使用 ImageViews 从我的资源中提取图像。我的问题是在我的适配器中传递给 getView() 方法的 convertVi
这是我为 ListView 创建自定义 View 的代码。它使用名为 SquareProgressBar 的自定义 View : package com.example.simpledownloade
在我的自定义 ListAdapter 中,第一次调用 GetView() 时,convertView 作为 NULL 传入,但第二次作为第一次创建的 View 传入。我的 ListView 有 4 行
在android中,我通常使用MyAdapter extends ArrayAdapter来为ListView创建 View ,结果,我不得不重写函数 public View getView(int
我有一个带有 viewList + 自定义适配器的 Activity 。 我想在运行时更新 viewList (当某些资源下载完成时使用回调)。我调用 ui-thread 来调用此方法: pu
你好安卓开发者,我已经阅读了有关 BaseAdapter 的 getView 方法的文档,我的理解是 View 可以重复使用/回收,因此在使用之前应该检查该 View 是否为非空且类型是否合适。在我的
我的问题是,当我滚动 ListView 时,我得到了错误的 convertView,但位置是正确的。 我的 Listview 中有 3 个项目,在加载时使用索引 '0' 调用“position”参数并
我是 Android 开发的新手,正在阅读一些示例代码。我从适配器类(派生自 ArrayAdapter)中的示例代码复制了一个方法,派生类除了 TextView 外还有一个复选框: @Override
假设这是我的代码,我想根据某些条件将 2 个不同的 View 膨胀到 listView。 public View getView(int position, View convertView, Vie
我一直在浏览示例和教程,但我似乎无法理解如何在子类化的 SimpleCursorAdapter 中处理回收。我知道对于常规 ArrayAdapter,您可以检查 convertView 是否为 nul
为什么 ListView 中的链接在滚动时丢失了?从调试中可以清楚地看出,不会在 convertView 的 TextView 上第二次添加跨度。 这是从适配器的 getView 调用的一段代码。
我是一名优秀的程序员,十分优秀!