2017-01-09 96 views
0

此對話框的列表視圖不會更新。在大多數關於這個的其他stackoverflow的帖子中,問題是適配器的相關數組更改引用,但我確保不要這樣做。是否有無法更新的另一種解釋?listView不更新

/** 
    * This class represents a dialog fragment that appears on clicking the connect button of 
    * the main activity. The fragment displays a list of all discovered bluetooth devices. 
    */ 
public class DiscoveredDialogFragment extends DialogFragment { 


    public static DiscoveredDialogFragment newInstance() { 
     return new DiscoveredDialogFragment(); 
    } 

    public interface DiscoveredDialogListener { 
     public void onDeviceSelectedForConnection(String addressMac); 

     public void onScanClicked(); 
    } 

    @BindView(R.id.listview) 
    ListView mListView; 
    private SimpleAdapter mAdapter; 
    private ArrayList<HashMap<String, String>> mListDevice; 
    private DiscoveredDialogListener mDiscoveredDialogListener; 

    /** 
    * Display the view on on create. 
    * @param inflater 
    * @param container 
    * @param savedInstanceState 
    * @return 
    */ 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.dialog_fragment_devices_discovered, null); 
     getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
     getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0)); 
     getDialog().setCanceledOnTouchOutside(false); 
     ButterKnife.bind(this, view); 
     return view; 
    } 

    /** 
    * On activity being created, connect mListDevice arraylist with mListView. This will display 
    * the Name and MacAddress on each line of the listview. 
    * @param savedInstanceState 
    */ 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     mListDevice = new ArrayList<HashMap<String, String>>(); 
     mAdapter = new SimpleAdapter(this.getActivity(), mListDevice, 
       android.R.layout.two_line_list_item, 
       new String[]{"Name", "AddressMac"}, 
       new int[]{android.R.id.text1, android.R.id.text2}); 
     mListView.setAdapter(mAdapter); 
     mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 
       //create a new bluetooth client with the associated macaddress, and give this client 
       //its own thread 
       mDiscoveredDialogListener.onDeviceSelectedForConnection(mListDevice.get(position).get("AddressMac")); 
       dismiss(); 
      } 
     }); 
    } 

    public void setListener(DiscoveredDialogListener discoveredDialogListener) { 
     mDiscoveredDialogListener = discoveredDialogListener; 
    } 

    @Override 
    public void onDestroyView() { 
     super.onDestroyView(); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     EventBus.getDefault().register(this); 
     Log.e("EventBus", "registered"); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     EventBus.getDefault().unregister(this); 
    } 

    /** 
    * On clicking scan, discover all discoverable bluetooth devices 
    */ 
    @OnClick(R.id.scan) 
    public void scan() { 
     mDiscoveredDialogListener.onScanClicked(); 
    } 

    /** 
    * Update mListDevice with new bluetooth devices and their associated information. 
    * @param device 
    */ 
    @Subscribe 
    public void onEventMainThread(BluetoothDevice device) { 
     Log.e("BluetoothAdapter", "found a device"); 
     HashMap<String, String> item = new HashMap<String, String>(); 
     item.put("Name", device.getName()); 
     item.put("AddressMac", device.getAddress()); 
     if(!mListDevice.contains(item)){ 
      mListDevice.add(item); 
     } 

     mAdapter.notifyDataSetChanged(); 
    } 

編輯

原來,一切工作正常,但在列表視圖白色的文字寫在白色背景上。

+0

代碼看起來不錯,問題可能與listView的高度,將一些背景顏色放入列表視圖,並檢查它是否顯示在視圖中。 –

+1

原來,背景顏色確實是問題。該列表在白色背景上顯示白色文字:) – Chris

回答

0

SimpleAdapater不是爲使用更改的數據而構建的;它只處理靜態數據。所以,如果你想更新列表視圖,那麼你應該這樣

@Subscribe 
public void onEventMainThread(BluetoothDevice device) { 

    HashMap<String, String> item = new HashMap<String, String>(); 
    item.put("Name", device.getName()); 
    item.put("AddressMac", device.getAddress()); 
    if(!mListDevice.contains(item)){ 
     mListDevice.add(item); 
    } 
    mAdapter = new SimpleAdapter(this.getActivity(), mListDevice, 
      android.R.layout.two_line_list_item, 
      new String[]{"Name", "AddressMac"}, 
      new int[]{android.R.id.text1, android.R.id.text2}); 
    mListView.setAdapter(mAdapter); 
    mAdapter.notifyDataSetChanged(); 
} 

注意每次setAdapter因爲你的數據是動態的使用CustomAdapter增加你的ListView表演

0

適配器

SimpleAdapter 
     { 

     mListDevice = new ArrayList<HashMap<String, String>>(); 
     ... 


    // adapter method to call 
     public void updateData(ArrayList<HashMap<String,String>> mListDevice) 
     { 
     this.mListDevice =mListDevice ; 
     notifyDataSetChanged(); 
     } 

     } 

來自片段/活動的呼叫方法

HashMap<String, String> item = new HashMap<String, String>(); 
    item.put("Name", device.getName()); 
    item.put("AddressMac", device.getAddress()); 
mListDevice.add(item); 
mAdapter.updateData(mListDevice);