如何在蓝牙列表适配器中找到回路原因?

im是android studio的新功能,正在编写包含蓝牙功能的应用程序。当用蓝牙发现填充listview时,listadpater getview似乎为发现列表中的每个项目循环3倍。

我花了最后4个小时来追踪这个循环,包括android文档,各种没有答案的stackoverflow帖子(只有一个关闭了,唯一的建议是'不要担心:android: my ListAdapter duplicates loop 我在代码中放置了多条输出行,因此控制台准确显示了循环发生的位置。 我已经按照各种在线建议反复地重构了代码,但是我找不到为什么会发生循环,更不用说如何停止了。

如果代码或问题的格式不正确,我深表歉意:我的第一篇文章

广播接收方呼叫按钮功能和广播接收方

//discover devices
public void btnScanBtDev() {
    Log.d(TAG,"btnScabBtDev: looking for devices... ");

    //empty list if already populated (for more than one scan)
    mBTDevices = new ArrayList<>();

    //check if already in discovery mode
    if (mBluetoothAdapter.isDiscovering()) {
        mBluetoothAdapter.cancelDiscovery();
        Log.d(TAG,"btnDiscover: Canceling discovery.");
    }
    mBluetoothAdapter.startDiscovery();
    IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.actION_FOUND);
    registerReceiver(mBroadcastReceiver3,discoverDevicesIntent);
}



private BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context,Intent intent) {
        final String action = intent.getaction();
        Log.d(TAG,"broadcastReceiver3 (pairing): actION FOUND.");

        if (action.equals(BluetoothDevice.actION_FOUND)) {
            BluetoothDevice device =intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            System.out.println("new device object created");

            mBTDevices.add(device);
            Log.d(TAG,"broadcastReceiver3: BT_device name & address: "
                    + device.getName() + ": " + device.getaddress());
            mDeviceListAdapter = new DevLisAdap(context,R.layout.adapter_device_view,mBTDevices);

            System.out.println("setting adapter");
    //the loop occurs after this message so it must be executed by the following line?:
            lvNewDevices.setadapter(mDeviceListAdapter);
        }
    }
}

列表适配器类:

package com.example.bluetoothreceiver_v02;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;

public class DevLisAdap extends ArrayAdapter<BluetoothDevice> {
private static final String TAG = "DevListAdap";

private LayoutInflater mLayoutInflater;
private ArrayList<BluetoothDevice> mDevices;
private int mViewResourceId;

public DevLisAdap(Context context,int tvResourceId,ArrayList<BluetoothDevice> devices) {
    super(context,tvResourceId,devices);
    System.out.println("getview constructor");
    this.mDevices = devices;
    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INflaTER_SERVICE);
    mViewResourceId = tvResourceId;
}


//method seems to loop 3x for each discovered item?
public View getView(int position,View convertView,ViewGroup parent) {
    System.out.println("begin getview");
    //counter to prove if entire method is called each time
    int counter =0;
    System.out.println("counter: "+counter);

    View conView = convertView;
    if(conView==null){
        conView = mLayoutInflater.inflate(mViewResourceId,null);
        System.out.println("inflated");
    }
    BluetoothDevice device = mDevices.get(position);

    if (device != null) {
        Log.d(TAG,"getView: BT_device exists.");
        TextView deviceName = (TextView) conView.findViewById(R.id.tvDeviceName);
        TextView deviceAdress = (TextView) conView.findViewById(R.id.tvDeviceAddress);

        if (deviceName != null) {
            System.out.println("name set");
            deviceName.setText(device.getName());
        } else {
            Log.d(TAG,"getView: BT_device name is NULL!!");
        }
        if (deviceAdress != null) {
            System.out.println("add set");
            deviceAdress.setText(device.getaddress());
        } else {
            Log.d(TAG,"getView: BT_device address is NULL!!");
        }
    } else {
        Log.d(TAG,"BT_device is NULL!!");
    }
    System.out.println("end of getview");
    counter++;
    return conView;
}

}

输出(对于第一项,删除名称和地址):

D/Bluetoothactions: btnScanBtDev: looking for devices... 
D/Bluetoothactions: broadcastReceiver3 (pairing): actION FOUND.
I/System.out: new device object created
D/Bluetoothactions: broadcastReceiver3: BT_device name & address: xx
I/System.out: getview constructor
I/System.out: setting adapter
I/System.out: begin getview
counter: 0
I/System.out: inflated
I/System.out: name set
I/System.out: add set
I/System.out: end of getview
I/System.out: begin getview
counter: 0
name set
I/System.out: add set
end of getview
I/System.out: begin getview
I/System.out: counter: 0
name set
I/System.out: add set
I/System.out: end of getview
D/Bluetoothactions: broadcastReceiver3 (pairing): actION FOUND.
...etcetc

对于发现的每个项目,setadapter似乎会反复调用整个getView方法(3 *)(3 *)? (例如1个项目= 3个循环,4个项目= 3 + 6 + 9 + 12个循环= 30,依此类推) 尽管对于一小部分列表而言这不是问题,但这确实会很快导致大量迭代! 更不用说它令人发疯的无法看到它来自何处?我找不到这种解释的标准android行为吗?

eunsike 回答:如何在蓝牙列表适配器中找到回路原因?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3152266.html

大家都在问