java - List DataModel 只读取最后一个元素

标签 java android mysql sql sqlite

我有一个用位置更新创建的数据库,数据库中有一堆位置 x 和 y。在第二种方法中,readFirestore() 读取位置数据并比较来自 sqlite 数据库的最喜欢的位置,如果最喜欢的位置靠近来自 firestore 的数据,它会将位于同一位置的 Activity 名称写入另一个数据库。但是当我想比较 firestore 方法中最喜欢的位置时,只有数据库的最后一项。我看日志。

代码 1:

public List<DataModel> listFavoriteLocation(){
    db = new DatabaseHelper(this);
    SQLiteDatabase mydb = db.getWritableDatabase();
    List<DataModel> data=new ArrayList<>();
    Cursor csr = mydb.rawQuery("select * from "+TABLE+" ;",null);
    StringBuffer stringBuffer = new StringBuffer();
    DataModel dataModel = null;
    while (csr.moveToNext()) {
        dataModel= new DataModel();
        String FAVCurrentLocationLAT = csr.getString(csr.getColumnIndexOrThrow("FAVCurrentLocationLAT"));
        String FAVCurrentLocationLONG = csr.getString(csr.getColumnIndexOrThrow("FAVCurrentLocationLONG"));
        dataModel.setFAVCurrentLocationLAT(FAVCurrentLocationLAT);
        dataModel.setFAVCurrentLocationLONG(FAVCurrentLocationLONG);
        stringBuffer.append(dataModel);
        data.add(dataModel);

    }
    for (DataModel mo:data ) {
        this.List_FAVCurrentLocationLAT = mo.getFAVCurrentLocationLAT();
        this.List_FAVCurrentLocationLONG = mo.getFAVCurrentLocationLONG();
         Log.i("helloLAT",""+List_FAVCurrentLocationLAT); //OK
         Log.i("helloLONG",""+List_FAVCurrentLocationLONG); //OK
    // This section writes the favorite locations seperately to the log. 
     }
    return data;
}

代码 2:

public void readFirestore() {
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    db.collection("campaigns")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                private String FSname,FScityLAT,FScityLONG,FScampaignStartDate,FScampaignEndDate;

                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful() && task.getResult() != null) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            String name = document.getString("name");
                            String cityLAT = document.getString("cityLAT");
                            String cityLONG = document.getString("cityLONG");
                            String campaignStartDate = document.getString("campaignStartDate");
                            String campaignEndDate = document.getString("campaignEndDate");

                            this.FSname = name;
                            this.FScityLAT = cityLAT;
                            this.FScityLONG = cityLONG;
                            this.FScampaignStartDate = campaignStartDate;
                            this.FScampaignEndDate = campaignEndDate;

                            listFavoriteLocation();

                            String FS_FAVCurrentLocationLAT = List_FAVCurrentLocationLAT;
                            String FS_FAVCurrentLocationLONG = List_FAVCurrentLocationLONG;

                            Log.i("hellolist",""+List_FAVCurrentLocationLAT); // just writes the last loc item from sqlite

                            double FS_FAVCurrentLocationLAT_double = Double.parseDouble(FS_FAVCurrentLocationLAT); // Fav Loc DB
                            double FS_FAVCurrentLocationLONG_double = Double.parseDouble(FS_FAVCurrentLocationLONG);                                 double FScityLAT_double = Double.parseDouble(FScityLAT);  // Campaign Loc Firestore LAT
                            double FScityLONG_double = Double.parseDouble(FScityLONG);  

                            double theta = FScityLONG_double - FS_FAVCurrentLocationLONG_double;
                            double dist = Math.sin(Math.toRadians(FS_FAVCurrentLocationLAT_double)) * Math.sin(Math.toRadians(FScityLAT_double)) + Math.cos(Math.toRadians(FS_FAVCurrentLocationLAT_double)) * Math.cos(Math.toRadians(FScityLAT_double)) * Math.cos(Math.toRadians(theta));
                            dist = Math.acos(dist);
                            dist = Math.toDegrees(dist);
                            dist = dist * 60 * 1.1515;
                            dist = dist * 1.609344;

                            if (dist <= 0.5) // 500 meter
                            {
                                SQLiteQueryFavCampaign = "INSERT OR REPLACE INTO myTable3(FAVCampaignName, FAVCampaigncampaignStartDate, FAVCampaigncampaignEndDate)" + " VALUES('"+FSname+"','"+FScampaignStartDate+"','"+FScampaignEndDate+"');";
                                SQLITEDATABASEFavCampaign.execSQL(SQLiteQueryFavCampaign);
                                Log.i("helloname",""+FSname);
                            }
                        }
                    } else {
                    }
                }
            });

    Toast.makeText(CampaignActivity.this,"Creating", Toast.LENGTH_SHORT).show();
}

最佳答案

如果我理解正确:listFavoriteLocation 方法正确地检索了您期望从数据库中获取的数据。如果您查看其余代码,您会发现您正在迭代数据列表并用它们一个接一个地覆盖您的实例变量,直到列表被完全迭代,这意味着您离开方法后,只会保留实例中的最后一个元素。

因此,需要明确的是,以下 block 将正确记录每个元素,但只有最后一个元素的值将保留在您使用的两个实例变量中(FAVCurrentLocationLATFavCurrentLocationLong):

for (DataModel mo:data ) {
    this.List_FAVCurrentLocationLAT = mo.getFAVCurrentLocationLAT();
    this.List_FAVCurrentLocationLONG = mo.getFAVCurrentLocationLONG();
    Log.i("helloLAT",""+List_FAVCurrentLocationLAT); //OK
    Log.i("helloLONG",""+List_FAVCurrentLocationLONG); //OK
    // This section writes the favorite locations seperately to the log. 
}

您需要做的是使用返回的 data 列表加载到 listFavoriteLocation 方法中,然后根据需要在以下代码中对其进行操作。

所以,例如:

List<DataModel> data = listFavoriteLocation();
for (int i = 0; i < data.size(); i++) {
    DataModel dataModel = data.get(i);
    log.i("Data model "+i+": "+dataModel);
    // Do work on each data model element here
}

关于java - List DataModel 只读取最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55906675/

相关文章:

java - hibernate 4 jta websphere

java - 重置 Logback Appender

android - 如何在 Android 上管理 startActivityForResult

Android 不会执行我的 else 语句

java - 如果库存为 0,则尝试将商品的可用性设置为“否”且不可用(无法发行),但该商品似乎仍在发行

php - 如何创建多个游戏服务器?

java - ALOS 卫星产品到 PNG 转换问题(缺少旋转)

android - 将 LiveDataTestUtil 与 Kotest 结合使用

mysql - 如何在 BigQuery/SQL 中将行转置为具有大量数据的列?

java - java中的垃圾收集