如何从sqlite获取所有数据并一一计算

时间:2019-08-21 13:30:18

标签: java android sqlite hashmap calculation

我的表中有3个项目,我也使用HashMap。因此,我的表如下所示:

WIFI_TABLE              
position_id  routers                values  
        ssid    mac_id            mac_id    rssi
A          AP1  23:xx:xx:xx     23:xx:xx:xx -102
B          AP2  12:xx:xx:xx     12:xx:xx:xx -98
C          AP3  9a:xx:xx:xx     9a:xx:xx:xx -100

我也在HashMap中扫描新数据,看起来像这样:

SCAN RESULT             
position_id         routers     values  
            ssid    mac_id      mac_id  rssi
UNKNOWN   AP1   23:xx:xx:xx 23:xx:xx:xx -102
          AP2   12:xx:xx:xx 12:xx:xx:xx -98
          AP3   9a:xx:xx:xx 9a:xx:xx:xx -100
          AP4   76:xx:xx:xx 76:xx:xx:xx -108
          AP5   b8:xx:xx:xx b8:xx:xx:xx -80

如您所见,结果包含所有周围的数据。我只想计算相同的数据。

首先是从数据库获取数据。然后,如果扫描结果中的mac_id等于数据库中的mac_id,则计算scanned rssi - database rssi。例如,扫描的mac_id = 23:xx:xx:xx等于数据库AP1,即result = (-98) - (-102)。然后获取第二个数据,进行计算,获取result2,也获取第三个数据,并获取result3。

这是Position_data类:

public class PositionData implements Serializable {
private String name;
    public HashMap<String, Integer> values;
    public HashMap<String,String> routers;
    public PositionData(String name) {
        // TODO Auto-generated constructor stub
        this.name=name;
        values = new HashMap<String, Integer>();
        routers = new HashMap<String, String>();

    }
    public void addValue(Router router,int strength){

        values.put(router.getBSSID(), strength);
        routers.put(router.getBSSID(),router.getSSID());

    }
    public String getName() {
        return name;
    }
    public String toString() {
        String result="";
        result+=name+"\n";
        for(Map.Entry<String, Integer> e: this.values.entrySet())
                 result+=routers.get(e.getKey())+" : "+e.getValue().toString()+"\n";

        return result;  

    }
    public HashMap<String, Integer> getValues() {
        return values;
    }

    public HashMap<String, String> getRouters() {
        return routers;
    }

这是活动类:

PositionData positionData = (PositionData) intent
                .getSerializableExtra("PositionData");

positionsData=db.getReadings(building);

HashMap<String, Integer> rssi = positionData.getValues();
HashMap<String, Integer> rssi1 = positionsData.get(0).getValues();
HashMap<String, String> dest = positionsData.get(0).getRouters();

int dista =  0;
if (positionData.equals(dest)){
dista = Integer.parseInt(String.valueOf(rssi))-Integer.parseInt(String.valueOf(rssi1));
        }

        Log.v("dis:", String.valueOf(dista));

这也是从数据库助手类中获得的:

    public ArrayList<PositionData> getReadings(String building_id) {
        HashMap<String, PositionData> positions = new HashMap<String, PositionData>();
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select distinct * from " + READINGS_TABLE
                + " where building_id='" + building_id + "'", null);
        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {
            String position_id = cursor.getString(1);
            Router router = new Router(cursor.getString(2), cursor.getString(3));
            Log.v(cursor.getString(2), cursor.getInt(4) + "");
            if (positions.containsKey(position_id)) {

                positions.get(position_id).addValue(router, cursor.getInt(4));
            } else {
                PositionData positionData = new PositionData(
                        cursor.getString(1));
                positionData.addValue(router, cursor.getInt(4));
                positions.put(position_id, positionData);
            }
            cursor.moveToNext();

        }
        System.out.println("Reading done");
        ArrayList<PositionData> result = new ArrayList<PositionData>();
        for (Map.Entry<String, PositionData> e : positions.entrySet())
            result.add(e.getValue());
        return result;

    }

0 个答案:

没有答案