2012-09-08 89 views
2

我有兩個列表,使用相同的擴展BaseAdapter在相同的活動佈局,但只有一個更新時,我呼籲notifyDataSetChanged(我當然稱爲它)。我一直在尋找了幾天,無法發現問題。Android:擴展BaseAdapter與列表不更新

活動得到配對和可用的藍牙設備,並分別列出了他們,我用了BluetoothChat樣品,但我需要自定義行必須改變它的一些。

主要問題是標記有「這僅適用於當其他(正下方)的評論」,按Ctrl + F到那裏更快。

這裏是我的特殊BaseAdapter(可能是無用的,但我無論如何它張貼):

而且使用它的活動:

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); 
      } 
     } 
    }; 
} 

我評論的一切從可用設備列表確保已配對的設備列表已填充。通過猜我會說可能有一些失落參考回事,但我不完全瞭解列表是如何工作的但我找不到它。

問題是,當我嘗試填充兩個lits並調用.add()時,設備實際上被添加到列表中(.size()返回正確的數字),但第一個不會刷新。

回答

0

解決方案

當製作這種可重複使用的類作爲DeviceListBaseAdapter記得從未設置局部變量static。什麼static確實是到該對象的所有實例之間共享這個特別的變量,因此我有DeviceListBaseAdapter 2個實例,但只有一個deviceArrayList,這就是爲什麼只有一個列表,在工作一段時間。謹防快速複製粘貼。