RecyclerView 未使用 [WIFI DIRECT] 发现进行更新
RecyclerView not being updated with [WIFI DIRECT] discovery
我有一个具有特定 "device names" 的 database(SQLite)
,将添加到 RecyclerView
,同时根据设备状态将 TextView
更改为 Online/Offline .
当我执行 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();
不过好像也没什么用。
改变这件事..
mRecyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
当你想刷新或者你的列表正在更新任何修改时,只把这一行用于刷新回收器视图..
if(mAdapter!=null){
mAdapter.notifyDataSetChanged();
}
我有一个具有特定 "device names" 的 database(SQLite)
,将添加到 RecyclerView
,同时根据设备状态将 TextView
更改为 Online/Offline .
当我执行 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();
不过好像也没什么用。
改变这件事..
mRecyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
当你想刷新或者你的列表正在更新任何修改时,只把这一行用于刷新回收器视图..
if(mAdapter!=null){
mAdapter.notifyDataSetChanged();
}