Android 在ListView中重复的行

Android 在ListView中重复的行,android,listview,Android,Listview,我知道这个问题已经出现过很多次了,但由于某种原因,我似乎无法让它发挥作用。 在某些情况下多次调用getView 然而,在这里给出的示例中: 它表示数据中每一行的第一次调用应该在currentView中得到一个空值。 这并没有发生 对我来说,位置为0时的调用将currentView设置为null,而位置为1时的调用将currentView设置为现有对象 总共有16个对“getView”的调用,但我将行复制一次(即每行两次)。 第0行 一排 第0行 第1行 我可能只是不明白那篇文章的内容 布局: &

我知道这个问题已经出现过很多次了,但由于某种原因,我似乎无法让它发挥作用。 在某些情况下多次调用getView

然而,在这里给出的示例中: 它表示数据中每一行的第一次调用应该在currentView中得到一个空值。 这并没有发生

对我来说,位置为0时的调用将currentView设置为null,而位置为1时的调用将currentView设置为现有对象

总共有16个对“getView”的调用,但我将行复制一次(即每行两次)。 第0行 一排 第0行 第1行

我可能只是不明白那篇文章的内容

布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"
    >
    <TextView android:id="@+id/title_paired_devices" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_paired_devices" 
        android:visibility="gone"
        android:background="#666"
        android:textColor="#fff"
        android:paddingLeft="5dp"
    />
    <ListView android:id="@+id/paired_devices"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stackFromBottom="true"
        android:layout_weight="1"
    />
    <TextView android:id="@+id/title_new_devices"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_other_devices"
        android:visibility="gone"
        android:background="#666"
        android:textColor="#fff"
        android:paddingLeft="5dp"
    />
    <ListView android:id="@+id/new_devices"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stackFromBottom="true"
        android:layout_weight="2"
    />
    <Button android:id="@+id/button_scan" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_scan"
    />
</LinearLayout>
这是屏幕的外观:


有趣的是,虽然所有4个按钮都能工作,但上下文菜单只对前两个有效

一般来说,除了为每个项目调用常规的getView外,它的调用次数大约要多3倍,因为它需要计算滚动条大小、布局等视图的测量值。关于重复的行,我认为您应该粘贴代码。你可能做错了什么


有关更多信息,请查看并阅读
currentView
值应用于解决优化问题。Android建议重用视图以节省内存和更快的响应

当yor
currentView
不为空时,您可以使用它来显示当前项目。
请看一个例子:

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        // we can't reuse view and must read it from layout
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.whatever, null);
    }

   // ... use v as view for row
}

在向ArrayList添加值之前,只需清除ArrayList

在您的
if
条件下

if (pairedDevices.size() > 0) { 
 pairedDevicesList.clear();
 // other code as it is
}
您还可以在清除ArrayList之前放置一个日志

Log.i("Size","ArrayList Size = "+pairedDevicesList.size());

因此,您将了解更多信息。

填写完
PairedDeviceList后,尝试在适配器上调用
notifyDataSetChanged()

if (pairedDevices.size() > 0) {
    findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
    for (BluetoothDevice device : pairedDevices) {
        pairedDevicesList.add(MPGDeviceDetailsControl.getDeviceDetails(this, device.getAddress(), device.getName()));
    }
}
((BaseAdapder)getListAdapter()).notifyDataSetChanged();
或者在填充
pairedDevicesList
后创建适配器

另外,请尝试声明适配器的数据具有稳定的ID:

@Override
public boolean hasStableIds() {
    return true;
}

为什么不发布一些代码,这样我们就可以找出哪里出了问题
数据中每一行的第一次调用在currentView中应该得到一个空值。
这是不正确的。Android可能会对超过第一行的任何行使用Previous use视图,而且通常会这样做。只需通过更改给定视图的内容来重用该视图。添加了thr代码。希望有人能帮助我。需要给潜在的雇主看,还是不行。在我填充之前将其显示为空,然后在填充之后将其显示为2行。也许,您会以某种方式显示
。尝试将其从布局中删除一段时间。完美!这样做就指出了错误:newDeviceListView.setAdapter(newLazyAdapter(这个,pairedDeviceList));将错误的列表连接到视图。因此,您现在连接到列表的内容。。您是否替换了NewDeviceListView.setAdapter(新LazyAdapter(这个,PairedDeviceList));
if (pairedDevices.size() > 0) { 
 pairedDevicesList.clear();
 // other code as it is
}
Log.i("Size","ArrayList Size = "+pairedDevicesList.size());
if (pairedDevices.size() > 0) {
    findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
    for (BluetoothDevice device : pairedDevices) {
        pairedDevicesList.add(MPGDeviceDetailsControl.getDeviceDetails(this, device.getAddress(), device.getName()));
    }
}
((BaseAdapder)getListAdapter()).notifyDataSetChanged();
@Override
public boolean hasStableIds() {
    return true;
}