Skip to content
Advertisement

List DataModel reads just the last element

I have a database created with location updates and in the database there is a bunch of locations x and y. and in the second method readFirestore() reads the location data and compares the favorite locations which came from sqlite database and if the favorite location is near the data from firestore it writes the campaign name which is on the same location to another database. But when I want to compare the favorite location in the firestore methot, there is just the last item of the database. I looked with the Log.

Code 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;
}

Code 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();
}

Advertisement

Answer

If I understand correctly: the listFavoriteLocation method properly retrieves the data you’re expecting from the database. If you take a look at the rest of your code, you’ll see that you are iterating over the list of data and overwriting your instance variables with them, one-by-one, until the list has been fully iterated over, meaning you will only preserve the last element in your instance once you’ve left the method.

So, to be clear, the following block will properly log every element, but only the values of the last element will be preserved in the two instance variables you’re using (FAVCurrentLocationLAT and FavCurrentLocationLong):

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. 
}

What you need to do is use the returned data list being loaded in the listFavoriteLocation method, and then manipulate it in the following code as you wish.

So, for example:

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
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement