1

利用可能なすべての Bluetooth デバイスを一覧表示する ListFragment があります。ただし、アプリケーションでフラグメントを切り替えてから ListFragment に戻すと、アダプター内のすべてのデータはまだそこにありますが、リストには何も表示されません。コードは次のとおりです。

ArrayAdapter<String> listAdapter;
ArrayList<BluetoothDevice> allDevices;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
    listAdapter = new ArrayAdapter<String>(inflater.getContext(),
            android.R.layout.simple_list_item_1);  
    setListAdapter(listAdapter);
    if (allDevices != null) {
        for (BluetoothDevice device : allDevices) {
            listAdapter.add(device.getName()+ "\n" + device.getAddress());
        }
        listAdapter.notifyDataSetChanged();
    }
    return super.onCreateView(inflater, container, savedInstanceState);  
}

初めて ArrayAdapter にアイテムを追加し、notifyDataSetChanged を呼び出すと、完全に正常に動作します。ただし、ListFragment を離れて戻ってくると、以前と同じリストが表示されるはずですが、何も表示されません。以前のように ArrayAdapter にデータを再度追加しても、そうではありません。onCreateView 関数で ArrayAdapter にデータを追加している ArrayList が空ではありません。

アドバイスありがとうございます!

4

0 に答える 0