我正在尝试扫描Android wifi网络并将其存储在阵列列表中。我的目标如下。

1.扫描wifi网络并将其存储在ScanObject的ArrayList中

private List<ScanResult> results; // A list of ScanResult is given by system
private ArrayList<ScanObject> scanObjectList = new ArrayList<ScanObject>;

for(int i=0;i<results.size();i++){
         String mac = results.get(i).BSSID;
         int rssi = results.get(i).level;
         scanObject = new ScanObject(mac,rssi);
         scanObjectList.add(i,scanObject);
}


2.将ScanObject的ArrayList与系统时间一起存储在ScanInfo对象中

    long systemTime = System.currentTimeMillis();
    scanInfo = new ScanInfo(systemTime,scanObjectList);


3.将ScanInfo对象添加到ScanInfo对象的ArrayList中

private ArrayList<ScanInfo> listOfScanInfoObjects = new ArrayList<ScanInfo>();
listOfScanInfoObjects.add(scanInfo);


当我尝试显示listOfScanInfoObjects中的对象时,可以显示仅看到最后一次扫描。例如,如果执行以下操作,经过几次扫描后,我只会看到最后一个对象中的元素数量。所有其他人给我返回零值。我想了解我在这里做错了什么。

for(int q=0;q<listOfScanInfoObjects.size();q++) {
    System.out.println("SIZE: "+listOfScanInfoObjects.get(q).getScanObjects().size());
}


我的ScanObject类如下所示;

public class ScanObject {
private String BSSID; // MAC address of the AP
private int RSSI; // RSSI value in dBm

public ScanObject(String mac, int rssi) {
    setBSSID(mac);
    setRSSI(rssi);
    }
// setter methods
    public void setBSSID(String mac){
        this.BSSID = mac;
    }
    public void setRSSI(int rssi){
        this.RSSI = rssi;
    }
// getter methods
    public String getBSSID(){
        return this.BSSID;
   }
   public int getRSSI(){
        return this.RSSI;
   }
}


我的ScanInfo类如下所示;

public class ScanInfo {
   private long timeStamp;
   private ArrayList<ScanObject> scanObjectList;

   public ScanInfo(long time, ArrayList<ScanObject> scanList) {
      setTimeSamp(time);
      setScanObjects(scanList);
    }
   // setter methods
   public void setTimeSamp(long time){
      this.timeStamp = time;
   }
   public void setScanObjects(ArrayList<ScanObject> scanList){
      scanObjectList = new ArrayList<ScanObject>();
      this.scanObjectList = scanList;
   }

   // getter methods
   public long getTimeStamp(){
       return this.timeStamp;
   }
   public ArrayList<ScanObject> getScanObjects(){
       return this.scanObjectList;
   }
}


以下是整体代码。

public void main(){
  processRawResult();
  listOfScanInfoObjects.add(index,scanInfo);
  for(int q=0;q<listOfScanInfoObjects.size();q++) {
System.out.println("SIZE:"+listOfScanInfoObjects.get(q).getScanObjects().size());
        }

  scanObjectList.clear();
  index++;
}

// process results object
public void processRawResult(){
    long systemTime = System.currentTimeMillis();
    scanObjectList = new ArrayList<ScanObject>();
    for(int i=0;i<results.size();i++){
         String mac = results.get(i).BSSID;
         int rssi = results.get(i).level;
         scanObject = new ScanObject(mac,rssi);
         scanObjectList.add(i,scanObject);
    }
    scanInfo = new ScanInfo(systemTime,scanObjectList);
}


提前致谢。

最佳答案

@哈萨拉

 public void setScanObjects(ArrayList<ScanObject> scanList){
      scanObjectList = new ArrayList<ScanObject>();
      this.scanObjectList = scanList;
   }


在这里,您在这里引用传递的列表。因此,当您清除列表时,scanObjectList中的所有数据也会被清除,因为两者相同,所以ScanInfo列表中包含的数据也会被清除。

public void main(){
    final ScanInfo scanInfo = processRawResult();
    listOfScanInfoObjects.add(scanInfo);
    for(int q=0;q<listOfScanInfoObjects.size();q++) {
         System.out.println("SIZE:"+ listOfScanInfoObjects.get(q).getScanObjects().size());
     }

  scanObjectList.clear();
  index++;
}


因此,在setScanObjects(ArrayList scanList)方法中使用this.scanObjectList = scanList;代替this.scanObjectList.addAll(scanList);

10-05 19:40