2

我有两个列表BaseAdapter在相同的活动布局中使用相同的扩展,但是当我调用时只有一个更新notifyDataSetChanged(我当然都调用它)。我一直在寻找几天,找不到问题。

该活动获取已配对且可用的蓝牙设备并分别列出它们,我使用了BluetoothChat示例,但我需要自定义行并且必须对其进行一些更改。

主要问题是用“这只在另一个(就在下面)被评论时才有效”,Ctrl+F可以更快地到达那里。

这是我的特辑BaseAdapter(可能没用,但我还是把它贴出来了):

/**
 * Device list base adapter to show the devices in a custom ListView.
 */
public class DeviceListBaseAdapter extends BaseAdapter
{
    private static ArrayList<Device> deviceArrayList;

    private LayoutInflater mInflater;

    public DeviceListBaseAdapter(Context context, ArrayList<Device> results)
    {
        deviceArrayList = results;
        mInflater = LayoutInflater.from(context);
    }

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

    public Object getItem(int position)
    {
        return deviceArrayList.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;

        if(convertView == null)
        {
            convertView = mInflater.inflate(R.layout.device_row_view, null);
            holder = new ViewHolder();
            holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            holder.tvAddress = (TextView) convertView.findViewById(R.id.tvAddress);
            holder.tvSignal = (TextView) convertView.findViewById(R.id.tvSignal);

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvName.setText(deviceArrayList.get(position).getName());
        holder.tvAddress.setText(deviceArrayList.get(position).getAddress());
        if(!deviceArrayList.get(position).getSignal().equals("0"))
        {
            holder.tvSignal.setText(deviceArrayList.get(position).getSignal() + "dBm");
        }

        return convertView;
    }

    static class ViewHolder
    {
        TextView tvName;
        TextView tvAddress;
        TextView tvSignal;
    }
}

以及使用它的活动:

public class DeviceSelectActivity extends Activity
{
    private ArrayList<Device> devAvailableList, devPairedList;
    private DeviceListBaseAdapter devAvailableListAdapter, devPairedListAdapter;
    private ListView devAvailableListView, devPairedListView;

    private Button bFindDevices;
    private BluetoothAdapter bluetoothAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.device_select);

        // Setup Bluetooth devices lists with custom rows
        devPairedListView = (ListView) findViewById(R.id.lvPairedDevices);
        devPairedList = new ArrayList<Device>();
        devPairedListAdapter = new DeviceListBaseAdapter(this, devPairedList);
        devPairedListView.setAdapter(devPairedListAdapter);

        // I commented this to see the behavior with only the first list    
//      devAvailableListView = (ListView) findViewById(R.id.lvAvailableDevices);
//      devAvailableList = new ArrayList<Device>();
//      devAvailableListAdapter = new DeviceListBaseAdapter(this, devAvailableList);
//      devAvailableListView.setAdapter(devAvailableListAdapter);

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        // Register a receiver to handle Bluetooth actions
        registerReceiver(Receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
        registerReceiver(Receiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));

        startDiscovery();
    }

    public void startDiscovery()
    {
        // Show search progress
        bFindDevices.setText(R.string.searching);
        bFindDevices.setEnabled(false);

        // Remove title for available devices
        findViewById(R.id.tvAvailableDevices).setVisibility(View.GONE);

        // Get a set of currently paired devices
        devPairedList.clear();
        devPairedListAdapter.notifyDataSetChanged();

        Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
        if(pairedDevices.size() > 0)
        {
            findViewById(R.id.tvPairedDevices).setVisibility(View.VISIBLE);
            for(BluetoothDevice device : pairedDevices)
            {
                devPairedList.add(new Device(device.getName(), device.getAddress(), (short) 0));
                // This only works when the other (just below) is commented
                devPairedListAdapter.notifyDataSetChanged();
            }
        }

//      devAvailableList.clear();
//      devAvailableListAdapter.notifyDataSetChanged();

        bluetoothAdapter.startDiscovery();
    }

    // add found device to the devices list
    private final BroadcastReceiver Receiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();
            if(BluetoothDevice.ACTION_FOUND.equals(action))
            {
                // Found a device in range
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Device foundDevice = new Device(device.getName(), device.getAddress(), intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE));
                // If it's not a paired device add it to the list
                if(device.getBondState() != BluetoothDevice.BOND_BONDED)
                {
//                  devAvailableList.add(foundDevice);
                    // Signal list content change
//                  devAvailableListAdapter.notifyDataSetChanged();
                    // Make the available devices title visible
                    findViewById(R.id.tvAvailableDevices).setVisibility(View.VISIBLE);
                }
            }
            else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
            {
                // When finished (timeout) remove the progress indicator
                bFindDevices.setText(R.string.search);
                bFindDevices.setEnabled(true);
            }
        }
    };
}

我评论了可用设备列表中的所有内容,以确保已填充配对设备列表。猜测我会说可能有一些丢失的参考资料,但由于我不完全了解列表的工作原理,但我找不到它。

问题是,当我尝试填充两个灯并调用.add()设备时,它们实际上被添加到列表中(.size()返回正确的数字),但第一个没有被刷新。

4

1 回答 1

0

解决方案

在制作此类可重用类时,DeviceListBaseAdapter请记住永远不要将局部变量设置为static. 什么static是在该对象的所有实例之间共享这个特定变量,因此我有 2 个实例DeviceListBaseAdapter,但只有一个deviceArrayList,这就是为什么一次只有一个列表有效。当心快速复制粘贴。

于 2012-09-08T14:50:11.387 回答