首页 文章

RecyclerView未使用[WIFI DIRECT]发现进行更新

提问于
浏览 29
0

我有 database(SQLite) 具有特定的"device names",并将根据设备状态将 TextView 更改为在线/离线时添加到 RecyclerView .

设备状态是在我执行运行此按钮的 onClickListener 时确定的:

refreshBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(getApplicationContext(),"Scanning for devices",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(int reason) {
                Toast.makeText(getApplicationContext(),"Scanning failed",Toast.LENGTH_LONG).show();
            }
        });
    }
});

PeerListListener:

WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() {
        @Override
        public void onPeersAvailable(WifiP2pDeviceList peerList) {
            loadingBar.setVisibility(View.VISIBLE);
            if(!peerList.getDeviceList().equals(peers)){
                peers.clear();
                peers.addAll(peerList.getDeviceList());

                deviceNameArray = new String[peerList.getDeviceList().size()];
                deviceArray     = new WifiP2pDevice[peerList.getDeviceList().size()];
                int index       = 0;
                int dbindex;

                //loadingBar.setMax(deviceDetails.size());
                //loadingBar.setProgress(0);

                for(WifiP2pDevice device : peerList.getDeviceList()){
                    deviceNameArray[index] = device.deviceName;


                    System.out.printf(deviceNameArray[index]);


                    deviceArray[index]     = device;
                    for(dbindex = 0;dbindex<deviceDetails.size();dbindex++) {
                        if(!deviceDetails.get(dbindex).getStatus().equals("lock") || !deviceDetails.get(dbindex).getStatus().equals("maintenance")) {
                            if (deviceDetails.get(dbindex).getDeviceNo().equals(deviceNameArray[index])) {
                                deviceDetails.get(dbindex).setStatus("online");
                            } else {
                                deviceDetails.get(dbindex).setStatus("offline");
                            }
                        }
                    }
                    index++;
                    //loadingBar.setProgress(index);
                }
                mAdapter = new DeviceAdapter(UserMain.this,deviceDetails);
                mAdapter.notifyDataSetChanged();
                mRecyclerView.setAdapter(mAdapter);

//                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,deviceNameArray);
//                listView.setAdapter(adapter);
            }
            if(peers.size() == 0){
                Toast.makeText(getApplicationContext(),"No Device Found..",Toast.LENGTH_SHORT).show();
                return;
            }
            loadingBar.setVisibility(View.INVISIBLE);
        }
    };

我尝试打印deviceNameArray,设备确实显示在控制台名称上,但 RecycleView 根本没有更新 .

我的RecyclerView自定义适配器的一部分:

@NonNull
@Override
public DeviceAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.user_main_card,parent,false);

    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(@NonNull DeviceAdapter.ViewHolder holder, final int position) {
    holder.deviceNo.setText("No." +deviceDetails.get(position).getDeviceNo());

    if(deviceDetails.get(position).getStatus().equals("online")) {
        holder.deviceStatus.setText("ONLINE");
    }else if(deviceDetails.get(position).getStatus().equals("offline") || deviceDetails.get(position).getStatus().equals("maintenance")){
        if(deviceDetails.get(position).getStatus().equals("offline")){
            holder.deviceStatus.setText("OFFLINE");

        }else if(deviceDetails.get(position).getStatus().equals("maintenance")){
            holder.deviceStatus.setText("MAINTENANCE");

        }
    }



    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(deviceDetails.get(position).getStatus().equals("offline") || deviceDetails.get(position).getStatus().equals("maintenance")) {
                Toast.makeText(mContext, deviceDetails.get(position).getDeviceName(), Toast.LENGTH_SHORT).show();
            }
            if(deviceDetails.get(position).getStatus().equals("online")){
                ((UserMain)mContext).connect(deviceDetails.get(position).getDeviceNo());
            }
        }
    });
}

我试过用:

mAdapter.notifyDataSetChanged();

但它似乎没有帮助 .

1 回答

  • 0

    换这个东西..

    mRecyclerView.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();
    

    当你想刷新或你的列表更新任何修改,然后只将此行放入刷新回收视图..

    if(mAdapter!=null){ 
          mAdapter.notifyDataSetChanged();
         }
    

相关问题