laprof
laprof

Reputation: 1344

Android RecyclerView only displays items after closing and restarting the app

I have a strange effect with my RecyclerView after I updated my App to API 28. I have a search button that triggers a search of Wifi devices in my area. The items are no longer displayed directly in the list. I have to close and reopen the app to get the correct display.

I have a toast that should be displayed when the button is pressed. The toast message is also only displayed after closing and opening the app.

Activity.kt

   private fun setFloatingSearchButton() {
        floating_button_discover_devices.setOnClickListener { view ->
            Snackbar.make(view, "Search for new devices ...",
                    Snackbar.LENGTH_SHORT).setAction("Action", null).show()

            discoverDevices()

            registerReceiver(mWifiReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))

            mWifiManager!!.startScan()
        }
    }

WifiAdapter.java

public class WifiAdapter extends RecyclerView.Adapter<WifiAdapter.ViewHolder> {

    private final int CONFIGURE_DEVICE_REQUEST_CODE = 0x00000001;

    private static final String TAG = "WifiAdapter";

    private List<DeviceWifiTo> wifis;

    private Context context;
    private Activity activity;

    public WifiAdapter(List<DeviceWifiTo> wifis) {
        this.wifis = wifis;
    }

    @Override
    public WifiAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        activity = (Activity) context;
        LayoutInflater inflater = LayoutInflater.from(context);

        View wifiView = inflater.inflate(R.layout.item_wifi, parent, false);

        return new WifiAdapter.ViewHolder(wifiView);
    }

    @Override
    public void onBindViewHolder(WifiAdapter.ViewHolder holder, int position) {
        DeviceWifiTo wifi = wifis.get(position);
        Log.d(TAG, wifi.getSsid());
        TextView wifiName = holder.wifiName;
        wifiName.setText(wifi.getSsid());

        CardView cardView = holder.cardView;
        cardView.setOnClickListener(view -> {
            Intent intent = new Intent(context, ConfigureDeviceActivity.class);
            intent.putExtra("ssid", wifi.getSsid());
            activity.startActivityForResult(intent, CONFIGURE_DEVICE_REQUEST_CODE);
        });
    }

    @Override
    public int getItemCount() {
        return wifis.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView wifiName;
        CardView cardView;

        ViewHolder(View itemView) {
            super(itemView);
            wifiName = itemView.findViewById(R.id.text_wifi_name);
            cardView = itemView.findViewById(R.id.cardView_device);
        }
    }

}

UcDiscoverDevicesImpl.java

private void createRecyclerView(List<DeviceWifiTo> list) {
    RecyclerView recyclerView = activity.findViewById(R.id.recycler_view_wifi);
    WifiAdapter adapter = new WifiAdapter(list);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(activity.getApplicationContext()));
}

Upvotes: 1

Views: 963

Answers (4)

wilfred
wilfred

Reputation: 189

I had similar issue in kotlin just because of not adding the notifyDataSetChanged() where I am setting the data.

Upvotes: 0

Lakhwinder Singh
Lakhwinder Singh

Reputation: 7209

Try setting up layoutmanager before setting the adapter

Upvotes: 1

Dorokhov Yegor
Dorokhov Yegor

Reputation: 13

I guess you want to show your result in recyclerView. Do you have any callback methods for Your search?

You need to call notifyDataSetChanged() in that callback method (set result then call notifyDataSetChanged() )

Upvotes: 1

Jakir Hossain
Jakir Hossain

Reputation: 3930

Set layout manager before set adapter to recycler view, Update like below

private void createRecyclerView(List<DeviceWifiTo> list) {
   RecyclerView recyclerView = activity.findViewById(R.id.recycler_view_wifi);
   recyclerView.setLayoutManager(new LinearLayoutManager(activity.getApplicationContext()));
   WifiAdapter adapter = new WifiAdapter(list);
   recyclerView.setAdapter(adapter);

}

Hope it helps you.

Upvotes: 3

Related Questions