android - 什么决定了 getView() 的位置参数中的取值范围?

标签 android

我的适配器的 getView() 例程发生崩溃,因为它是用位置值 6 调用的,而我的数据源中只有 6 个项目。所以我假设位置参数应该在[0]-[5]的范围内?什么决定了 getView() 的位置参数的取值范围?

详细信息:

XML ...

<ListView
  android:id="@android:id/list"
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  android:cacheColorHint="@color/colGrey"
  android:background="@color/colGrey"
  android:clickable="true"
  android:fastScrollEnabled="true"
  android:choiceMode="none"/>

...在 MyListActivity 中,这是一个 ListActivity 。 . .

public static ListView lv;   // my ListView in the code

... 在 onCreate() 期间。 . .

   setContentView(R.layout.mylist);
   lv = getListView();

创建适配器并绑定(bind)它。 . .

mylistadapter = new MyListAdapter(MyListActivity.this);
setListAdapter(mylistadapter);   // bind the adapter

...数据源是一个名为 listItems 的 ArrayList。在运行程序的过程中,它的大小会发生变化,并且在程序执行时可能早了 15 秒。 . .

public static ArrayList<String>listItems=new ArrayList<String>();

... 在我的适配器中,它是一个 BaseAdapter,我对 getCount() 的验证看起来像这样。 . .

@Override
public int getCount() {
    return listItems.size();
}

...当我在 getView() 中调用 getCount() 时,它返回 6,这是数据源中的项目数,但如果我调用 lv.getCount(),它返回 15。(不知道这个 15 在哪里是从哪里来的?)这就是为什么适配器调用索引太大的 getView 的原因吗?

最佳答案

So I assumed that the position parameter should be in a range of [0]-[5]?

是的。

What determines the range of values in getView()'s position parameter?

范围从 0 到 getCount()-1,其中 getCount()Adapter 上实现。

during the course of running the program its size varies and it may have been 15 earlier in program execution

如果更改数据(包括更改行数),则需要在 Adapter 上调用 notifyDataSetChanged()

关于android - 什么决定了 getView() 的位置参数中的取值范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23685578/

相关文章:

android - 如何使用 intellij idea 调试已签名的应用程序?

java - 无法在Android上的外部存储上创建文件夹

Android LinearLayout 右对齐

android - 启动器未返回到我的应用程序中上次使用的 Activity

android - 邮寄到 Android : 'Unsupported action' error

android - 在简单的 Android 计算器应用程序中处理用户错误

android - Android 示例中的上下文问题 "Google Map View"

android - CSS固定位置div在Android中不可点击

java - 每天执行android studio

java - Epson SDK 版本 2 收银抽屉适用于 Android?