ListView 中重复的行

2024-04-05

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

但是,在此处给出的示例中:http://android.amberfog.com/?p=296 http://android.amberfog.com/?p=296它表示对数据中每一行的第一次调用应该在 currentView 中获得空值。 这并没有发生。

对我来说发生的事情是,当位置为 0 时,调用将 currentView 设置为 null,而当位置为 1 时,调用将 currentView 设置为现有对象。

总共对“getView”进行了 16 次调用,但我得到的行重复一次(即每行两次)。 第 0 行 第 1 行 第 0 行 第 1 行

我可能只是不明白那篇文章中的某些内容。

Layout:

<?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>

列表视图行:

    <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.

android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!--  ListRow Left side Thumbnail image -->
    <LinearLayout android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:layout_alignParentLeft="true"
        android:background="@drawable/image_bg"
        android:layout_marginRight="5dip">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:src="@drawable/icon"/>

    </LinearLayout>

    <!-- Device Address -->
    <TextView
        android:id="@+id/tvwDeviceAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="15dip"
        android:textStyle="bold"/>

    <!-- Display Name -->
    <TextView
        android:id="@+id/tvwDisplayName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tvwDeviceAddress"
        android:textColor="#343434"
        android:textSize="10dip"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"/>


</RelativeLayout>

Adapter:

    private class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<MPGDeviceDetails> data;
    private LayoutInflater inflater=null;

    public LazyAdapter(Activity a, ArrayList<MPGDeviceDetails> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        Log.e("MPG","Position = " + position + ".  ConvertView = " + convertView);
        if(convertView==null)
            vi = inflater.inflate(R.layout.device_name, null);

        TextView address = (TextView)vi.findViewById(R.id.tvwDeviceAddress); // Device Address
        TextView name = (TextView)vi.findViewById(R.id.tvwDisplayName); // Display name
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image



        address.setText(data.get(position).deviceAddress);
        name.setText(data.get(position).getDisplayName());
        Bitmap photo = data.get(position).getContactPhoto();
        if (photo != null)
            thumb_image.setImageBitmap(photo);


        return vi;
    }
}

Usage:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Setup the window
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.device_list);

    // Set result CANCELED incase the user backs out
    setResult(Activity.RESULT_CANCELED);

    // Initialize the button to perform device discovery
    Button scanButton = (Button) findViewById(R.id.button_scan);
    scanButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            doDiscovery();
            v.setVisibility(View.GONE);
        }
    });

    // Initialize array adapters. One for already paired devices and
    // one for newly discovered devices


    // Find and set up the ListView for paired devices
    pairedListView = (ListView) findViewById(R.id.paired_devices);
    pairedDevicesList = new ArrayList<MPGDeviceDetails>();
    pairedListView.setAdapter((mPairedDevicesArrayAdapter = new LazyAdapter(this,pairedDevicesList)));
    pairedListView.setOnItemClickListener(mDeviceClickListener);
    registerForContextMenu(pairedListView);

    // Get the local Bluetooth adapter
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();

    // Get a set of currently paired devices
    Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();

    // If there are paired devices, add each one to the ArrayAdapter
    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()));
        }
    } 

}

MPGDeviceDetails 是一个保存有关特定设备的所有数据的对象。

这是“debug”命令的输出:

03-27 10:03:30.730: E/MPG(2841): Position = 0.  ConvertView = null.
03-27 10:03:30.742: E/MPG(2841): Position = 1.  ConvertView = android.widget.RelativeLayout@4062d608. 
03-27 10:03:30.746: E/MPG(2841): Position = 0.  ConvertView = null. 
03-27 10:03:30.750: E/MPG(2841): Position = 1.  ConvertView = android.widget.RelativeLayout@40628d78. 
03-27 10:03:30.750: E/MPG(2841): Position = 0.  ConvertView = android.widget.RelativeLayout@4062d608. 
03-27 10:03:30.753: E/MPG(2841): Position = 1.  ConvertView = android.widget.RelativeLayout@4062d608. 
03-27 10:03:30.753: E/MPG(2841): Position = 0.  ConvertView = android.widget.RelativeLayout@40628d78. 
03-27 10:03:30.753: E/MPG(2841): Position = 1.  ConvertView = android.widget.RelativeLayout@40628d78. 
03-27 10:03:30.761: E/MPG(2841): Position = 1.  ConvertView = android.widget.RelativeLayout@4062d608. 
03-27 10:03:30.761: E/MPG(2841): Position = 0.  ConvertView = null. 
03-27 10:03:30.769: E/MPG(2841): Position = 1.  ConvertView = android.widget.RelativeLayout@40628d78. 
03-27 10:03:30.769: E/MPG(2841): Position = 0.  ConvertView = null. 
03-27 10:03:30.777: E/MPG(2841): Position = 0.  ConvertView = null. 
03-27 10:03:30.781: E/MPG(2841): Position = 1.  ConvertView = android.widget.RelativeLayout@406d4af8. 
03-27 10:03:30.781: E/MPG(2841): Position = 0.  ConvertView = null. 
03-27 10:03:30.785: E/MPG(2841): Position = 1.  ConvertView = android.widget.RelativeLayout@40694970. 
03-27 10:03:30.789: E/MPG(2841): Position = 0.  ConvertView = android.widget.RelativeLayout@406d4af8. 
03-27 10:03:30.789: E/MPG(2841): Position = 1.  ConvertView = android.widget.RelativeLayout@406d4af8. 
03-27 10:03:30.789: E/MPG(2841): Position = 0.  ConvertView = android.widget.RelativeLayout@40694970. 
03-27 10:03:30.792: E/MPG(2841): Position = 1.  ConvertView = android.widget.RelativeLayout@40694970. 

This is what the screen looks like: enter image description here

有趣的是,虽然所有 4 个按钮都有效,但上下文菜单仅适用于前两个按钮!


尝试打电话notifyDataSetChanged()完成填充后,在适配器上pairedDevicesList:

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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ListView 中重复的行 的相关文章

  • 定期运行任务(每天一次/每周一次)

    我想定期 每周 每天一次 运行一些任务 即获取我的网站新闻页面 即使我的应用程序已关闭 是否可以 是的 您需要查看报警管理器 http developer android com reference android app AlarmMan
  • 显示警报或收到通知时的视图

    我正在关注this http tokudu com 2010 how to implement push notifications for android 显示的教程通知 on an 安卓设备 当我在设备上运行该应用程序时 状态栏上会出现
  • 检索子值 -firebase-

    System out println ref child email protected cdn cgi l email protection child email 我正在尝试获取 child 的值 但我始终获取该值的 URL 当我尝试使
  • Android Lollipop prepareAsync() 需要很长时间才能返回

    在 Samsung Galaxy Note 4 上的 Android Lollipop 几周前刚刚从 4 4 4 更新 上 prepareAsync 几乎需要 20 秒来加载实时流 在 4 4 4 上 只需要 2 3 秒 并且没有错误 见下
  • 连接到不可发现的蓝牙设备

    我正在开发一个安卓应用程序 只是一个一般性问题 是否可以连接到公开不可发现的设备 提前致谢 如果您之前已与该设备配对 则即使该设备未处于可发现模式 也可以再次连接到该设备 参见这篇文章 以编程方式连接到配对的蓝牙设备 https stack
  • 在 WebView 中完成 AdBlock

    我即将在我的 Android 应用程序中推出 WebView AdBlocking 我想知道这是否会有效地阻止广告 或者在 Webview 本身内是否还有更多工作要做 我尚未修改 基本上我有一个存储在 Android 资产中的主机文件 其中
  • 监听什么来检测 Android 中的请勿打扰模式更改?

    我希望我的应用程序在手机设置为请勿打扰模式 仅限闹钟 仅限优先级或完全静音 时显示通知 通过聆听 这效果非常好android media RINGER MODE CHANGED在快速设置中检查此模式并在已选择的选项卡中选择模式时 但是 当选
  • Android Studio - 如何关闭“单词‘word’中的拼写错误?”

    当命名变量或给出字符串参数时 Android Studio 似乎对我如何标记事物有问题 有办法把它关掉吗 是的 打开Preferences gt Editor gt Inspections gt Spelling gt 关闭Typo并按OK
  • 从多个选项卡中的编辑文本字段获取文本

    我正在尝试创建一个使用选项卡作为输入表单的 Android 应用程序 基本上 我希望对其进行设置 以便用户可以在一个选项卡上输入一些信息 然后提交该信息 或者转到另一个选项卡并输入更多信息 然后从两个选项卡提交信息 我正在使用操作栏和片段来
  • 从手机访问本地主机[关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 我正在使用
  • 删除Android所有语言中的字符串

    我有一个包含多个翻译的应用程序 我想删除一些字符串 我怎样才能重构并删除它们一次 例如在默认情况下strings xml文件并自动将删除传播到其他翻译的其他 strings xml 文件 您可以通过 Android Studio 中的 翻译
  • 如何获取android手机型号、版本、sdk详细信息?

    如何获取android手机型号 版本 sdk详细信息 首先 看看 android sdk 页面上的这些 Build 类 http developer android com reference android os Build html h
  • 检测 ListView(或 ScrollView)内的滚动位置

    我正在构建一个聊天室应用程序 其中每 X 秒就会轮询一次新事件 每次发生这种情况时 此代码都会使用新数据更新 RoomAdapter ArrayAdapter 的自定义子类 并将其滚动到底部 RoomAdapter adapter Room
  • 在 Android 中上传文件出现内存不足错误

    我的上传代码如下 String end r n String twoHyphens String boundary try URL url new URL ActionUrl HttpURLConnection con HttpURLCon
  • “无法实例化活动”错误

    我的一个 Android 应用程序拥有大约 100 000 个用户 每周大约 10 次 我会通过 Google 的市场工具向我报告以下异常情况 java lang RuntimeException Unable to instantiate
  • 更改Android菜单的背景颜色[重复]

    这个问题在这里已经有答案了 我正在尝试将标准浅灰色更改为浅绿色 似乎没有一个简单的方法可以做到这一点 例如 通过 Android 主题 但我找到了一个解决方法 如本页所述 http tinyurl com 342dgn3 http tiny
  • 如何在Android中解析xml类型的HTTPResponse

    我有一个 Android 应用程序 我使用 POST 方法来获取响应 这是我的代码 HttpResponse httpResponse httpclient execute httppost HttpEntity resEntity htt
  • 在状态栏下方显示DialogFragment内容

    我试图显示高度和宽度均具有 match parent 的 DialogFragment 但碰巧在顶部 DialogFragment 显示在 StatusBar 下方 DialogFragment 正在应用一些默认值来填充底部 右侧 左侧和顶
  • putFragment() - 片段 x 当前不在 FragmentManager 中

    上面的标题被问了很多次 但答案似乎与FragmentStatePagerAdapter这与我的问题无关 我正在使用该方法putFragment Bundle String Fragment 直接地 The 安卓文档 http develop
  • 异步更新后更新Android Listview

    我正在将 HTTP 调用从同步调用转换为异步调用 由于连接在后台运行 因此当我最初设置列表适配器时 数据不存在 如何在 HTTP 调用后更新列表适配器 我尝试了一些方法 例如在数据发送回之前不设置适配器并再次设置适配器 但没有任何效果 这是

随机推荐