第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

List DataModel 只讀取最后一個元素

List DataModel 只讀取最后一個元素

滄海一幻覺 2022-10-26 16:43:32
我有一個使用位置更新創(chuàng)建的數(shù)據(jù)庫,并且在數(shù)據(jù)庫中有一堆位置 x 和 y。在第二種方法中,readFirestore() 讀取位置數(shù)據(jù)并比較來自 sqlite 數(shù)據(jù)庫的最喜歡的位置,如果最喜歡的位置靠近來自 firestore 的數(shù)據(jù),它會將位于同一位置的活動名稱寫入另一個數(shù)據(jù)庫。但是當我想比較firestore方法中最喜歡的位置時,數(shù)據(jù)庫中只有最后一項。我看了看日志。代碼 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;}
查看完整描述

1 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

如果我理解正確:該listFavoriteLocation方法正確地從數(shù)據(jù)庫中檢索您期望的數(shù)據(jù)。如果您查看其余代碼,您會發(fā)現(xiàn)您正在迭代數(shù)據(jù)列表并用它們一個接一個地覆蓋您的實例變量,直到列表被完全迭代,這意味著您離開方法后,只會保留實例中的最后一個元素。


因此,需要明確的是,以下塊將正確記錄每個元素,但只有最后一個元素的值將保留在您正在使用的兩個實例變量中(FAVCurrentLocationLAT和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. 

}

您需要做的是使用方法data中加載的返回列表listFavoriteLocation,然后根據(jù)需要在以下代碼中對其進行操作。


因此,例如:


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

}


查看完整回答
反對 回復 2022-10-26
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號