Задать вопрос
@seri0099
Начинающий программист

Как конвертировать ListView в RecyclerView?

У меня есть код с listview но хочу этот код конвертировать в recyclerview можете помочь?

вот код
public class UPnPScannerFragment extends Fragment {
    private ListView deviceList;

    private static ArrayAdapter<DeviceDisplay> listAdapter;

    private BrowseRegistryListener registryListener = new BrowseRegistryListener();

    private static AndroidUpnpService upnpService;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_upnp_scanner, null);
            deviceList = (ListView) v.findViewById(R.id.listView1);

        startUpnpService();

        return v;
    }



    private void startUpnpService() {

        listAdapter = new ArrayAdapter<DeviceDisplay>(getContext(), android.R.layout.simple_list_item_1);
        deviceList.setAdapter(listAdapter);
        deviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                AlertDialog dialog = new AlertDialog.Builder(v.getContext(), R.style.MyAlertDialogStyle)
                        .create();
                dialog.setTitle(R.string.deviceDetails);
                DeviceDisplay deviceDisplay = (DeviceDisplay) deviceList
                        .getItemAtPosition(position);

                dialog.setMessage(deviceDisplay.getDetailsMessage());
                dialog.show();
                TextView textView = (TextView) dialog
                        .findViewById(android.R.id.message);
                textView.setTextSize(12);

            }
        });
        getContext().getApplicationContext().bindService(
                new Intent(getContext(), AndroidUpnpServiceImpl.class),
                serviceConnection, Context.BIND_AUTO_CREATE);
    }



    private ServiceConnection serviceConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className, IBinder service) {
            upnpService = (AndroidUpnpService) service;
            listAdapter.clear();
            upnpService.getRegistry().addListener(registryListener);
            for (Device<?, ?, ?> device : upnpService.getRegistry()
                    .getDevices()) {
                registryListener.deviceAdded(device);
            }

            upnpService.getControlPoint().search();
        }

        public void onServiceDisconnected(ComponentName className) {
            upnpService = null;
        }
    };

    protected class BrowseRegistryListener extends DefaultRegistryListener {

        /* Discovery performance optimization for very slow Android devices! */
        @Override
        public void remoteDeviceDiscoveryStarted(Registry registry,
                                                 RemoteDevice device) {
            deviceAdded(device);
            if (device.getType().toString()
                    .equals("urn:dial-multiscreen-org:device:dial:1")) {
            }
        }

        @Override
        public void remoteDeviceDiscoveryFailed(Registry registry,
                                                final RemoteDevice device, final Exception ex) {
            if (device.getType().toString()
                    .equals("urn:dial-multiscreen-org:device:dial:1")) {
                deviceAdded(device);
            } else {
                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(getContext(),
                                "Discovery failed of '"
                                        + device.getDisplayString()
                                        + "': "
                                        + (ex != null ? ex.toString()
                                        : "Couldn't retrieve device/service descriptors"),
                                Toast.LENGTH_LONG).show();
                    }
                });
                deviceRemoved(device);
            }
        }

		/*
         * End of optimization, you can remove the whole block if your Android
		 * handset is fast (>= 600 Mhz)
		 */

        @Override
        public void remoteDeviceAdded(Registry registry, RemoteDevice device) {
            deviceAdded(device);
        }

        @Override
        public void remoteDeviceRemoved(Registry registry, RemoteDevice device) {
            deviceRemoved(device);
        }

        @Override
        public void localDeviceAdded(Registry registry, LocalDevice device) {
            deviceAdded(device);
        }

        @Override
        public void localDeviceRemoved(Registry registry, LocalDevice device) {
            deviceRemoved(device);
        }

        public void deviceAdded(final Device<?, ?, ?> device) {
            if (getActivity() != null) {
                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        DeviceDisplay d = new DeviceDisplay(device);
                        int position = listAdapter.getPosition(d);
                        if (current_section == 1) {
                            if (!(device
                                    .getType()
                                    .toString()
                                    .contains(
                                            "urn:schemas-upnp-org:device:InternetGatewayDevice") && device
                                    .isFullyHydrated())) {
                                return;
                            }
                        } else if (current_section == 2) {
                            if (!(device
                                    .getType()
                                    .toString()
                                    .contains(
                                            "urn:schemas-upnp-org:device:MediaServer") && device
                                    .isFullyHydrated())) {
                                return;
                            }
                        } else if (current_section == 3) {
                            if (!(device
                                    .getType()
                                    .toString()
                                    .contains(
                                            "urn:schemas-upnp-org:device:MediaRenderer") && device
                                    .isFullyHydrated())) {
                                return;
                            }
                        } else if (current_section == 4) {
                            if (!(device.getType().toString()
                                    .contains("urn:dial-multiscreen-org:device:dial:1"))) {
                                return;
                            }
                        }
                        if (position >= 0) {
                            listAdapter.remove(d);
                            listAdapter.insert(d, position);
                        } else {
                            listAdapter.add(d);
                        }
                    }
                });
            }
        }


        public void deviceRemoved(final Device<?, ?, ?> device) {

            if (getActivity() != null) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        listAdapter.remove(new DeviceDisplay(device));
                    }
                });
            }


        }
    }

    protected class DeviceDisplay {

        Device<?, ?, ?> device;

        public DeviceDisplay(Device<?, ?, ?> device) {
            this.device = device;
        }

        public Device<?, ?, ?> getDevice() {
            return device;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;
            DeviceDisplay that = (DeviceDisplay) o;
            return device.equals(that.device);
        }

        @Override
        public int hashCode() {
            return device.hashCode();
        }

        @Override
        public String toString() {
            String name = getDevice().getDetails() != null
                    && getDevice().getDetails().getFriendlyName() != null ? getDevice()
                    .getDetails().getFriendlyName() : getDevice()
                    .getDisplayString();

            if (!device.getType().toString()
                    .contains("urn:dial-multiscreen-org:device:dial:1"))
                return device.isFullyHydrated() ? name : name + "";
            else
                return name;
        }
    }

    public static void scanNetwork() {
        if (upnpService != null) {
            listAdapter.clear();
            upnpService.getRegistry().removeAllRemoteDevices();
            upnpService.getControlPoint().search();
        }
    }

}
  • Вопрос задан
  • 103 просмотра
Подписаться 1 Средний 1 комментарий
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы