繁体   English   中英

SimpleAdapter arraylist在每一行中显示相同的数据

[英]SimpleAdapter arraylist showing same data in each row

我有四个用于测试的蓝牙设备,用于获取蓝牙设备的名称和地址,分别称为deviceA,deviceB,deviceC和deviceD
我的程序可以通过mReceiver一对一地获得设备,但是SimpleAdapter数组列表行有问题。 当我扫描新设备时,所有行将更改为相同的名称和地址。 我在哪里找不到问题。 请帮忙,谢谢。

我的问题例如:

首先扫描deviceA时,结果为

设备A地址A

secound扫描deviceB时,结果为

设备B地址B

设备B地址B

第三次扫描deviceC时,结果为deviceC AddressC

deviceC地址C

deviceC地址C

第四次再次扫描deviceB时,结果为

设备B地址B

设备B地址B

设备B地址B

设备B地址B

扫描deviceD时,结果为

deviceD地址D

deviceD地址D

deviceD地址D

deviceD地址D

deviceD地址D

这是我的代码:

private static BluetoothAdapter mBluetoothAdapter = null;
private static BluetoothSocket mBluetoothSocket = null;
private ListView listView;
private ArrayList<Map<String,Object>> list = null;
private SimpleAdapter adapter = null; 
private Map<String,Object> hashmap = new HashMap<String,Object>(); 

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
            setContentView(R.layout.getbluetooth);
            listView = (ListView) findViewById(R.id.listView1); 
            list = new ArrayList<Map<String,Object>>(); 
            find_bluetooth();     
}

public void find_bluetooth()
{ 
        Set<BluetoothDevice> setPairedDevices = mBluetoothAdapter.getBondedDevices();

       IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
   registerReceiver(mReceiver, filter); 

       adapter = new impleAdapter(Diver_getEvents.this,list,R.layout.bluetooth_list,new String[]{"devicename","deviceaddress"},new int[]{R.id.devicename,R.id.deviceaddress});

       listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
        @Override
     public void onItemClick(AdapterView<?> arg0, View v, int position,long  id) 
        {
                      mBluetoothAdapter.cancelDiscovery();
                    }
        }

   private final BroadcastReceiver mReceiver = new BroadcastReceiver()
         {
            public void onReceive(Context context, Intent intent) 
          {
               String action = intent.getAction();
                if (BluetoothDevice.ACTION_FOUND.equals(action)) 
              {
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
               String deviceBTName = device.getName();
    String deviceBaddress = device.getAddress();
    hashmap.put("devicename",deviceBTName);  
    hashmap.put("deviceaddress",deviceBaddress);  
    list.add(hashmap); 
               listView.setAdapter(adapter);
              }
         }
       }

}

您每次将其添加到列表时都必须创建一个新的HashMap

hashmap = new HashMap<String,Object>()
// put some values to hashmap
...
...
list.add(hashmap)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM