2017-12-10 14 views
0

私は、気象情報をSQLiteデータベースに保存し、RecyclerViewに表示するアプリケーションを持っています。データベースを更新し、RecyclerViewをテーブルの更新された内容でリフレッシュする関数を実装しようとしています。RecyclerViewは最初のものではなく2番目のボタンで更新します

これは、ユーザーが「更新」ボタンをクリックしたときに発生するように実装しました。しかし、RecyclerViewはボタンの2回目のクリックでのみ更新されます。最初のクリックで更新されない理由はわかりません。

以下のクラスは、気象データをAPIから取得して正常に解析する非同期タスクです。 "onPostExecute"ステートメントは、データベースの値を更新する場所です。

refresh.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        updateCurrentWeather("Madrid"); 
        recyclerView.setLayoutManager(new LinearLayoutManager(FavouritesActivity.this)); 
        mAdapter = new CurrentWeatherAdapter(FavouritesActivity.this, getAllItems()); 
        recyclerView.setAdapter(mAdapter); 
        getAllItems(); 

       } 
      } 
    ); 

getAllItemsというメソッド()メソッド:クリックリスナーに

public class UpdateWeather extends AsyncTask<String, Void, CurrentWeather> { 

    @Override 
    protected CurrentWeather doInBackground(String... strings) { 
     //CONNECTION CLASS 
     //connect to API and download JSON data 
     String data = ((new HTTPClient()).getForecastData(strings[0])); 
     currentWeather = CurrentWeatherParser.getCurrentWeather(data); //Use the Parser class to get relevant data from the JSON 

     return currentWeather; 
    } 

    @Override 
    protected void onPostExecute(CurrentWeather currentWeather) { 
     super.onPostExecute(currentWeather); 
     ContentValues cv = new ContentValues(); 
     cv.put(CurrentWeatherContract.CurrentWeatherEntry.COL_0, currentWeather.currentCondtion.getCityName()); 
     cv.put(CurrentWeatherContract.CurrentWeatherEntry.COL_1, "countrycodetest"); 
     cv.put(CurrentWeatherContract.CurrentWeatherEntry.COL_7, "test3"); 
     cv.put(CurrentWeatherContract.CurrentWeatherEntry.COL_5, "sunrisetest"); 
     cv.put(CurrentWeatherContract.CurrentWeatherEntry.COL_6, currentWeather.currentCondtion.getSunset()); 
     cv.put(CurrentWeatherContract.CurrentWeatherEntry.COL_8, "test4"); 
     cv.put(CurrentWeatherContract.CurrentWeatherEntry.COL_2, "test2"); 
     cv.put(CurrentWeatherContract.CurrentWeatherEntry.COL_3, 2.33); 
     cv.put(CurrentWeatherContract.CurrentWeatherEntry.COL_4, 23.3); 

     //update table 
     myDB.update(CurrentWeatherContract.CurrentWeatherEntry.TABLE_NAME, cv, "City_Name = ?",new String[] {currentWeather.currentCondtion.getCityName()}); 

    } 
} 

//execute the async task based on a city name. 
public void updateCurrentWeather(String city) { 
    FavouritesActivity.UpdateWeather updateWeather = new FavouritesActivity.UpdateWeather(); 
    updateWeather.execute(Utilities.BASEURL_CR + city + Utilities.APIKEY); 
} 

マイ

private Cursor getAllItems() 
{ 
    return myDB.query(
      CurrentWeatherContract.CurrentWeatherEntry.TABLE_NAME, 
      null, 
      null, 
      null, 
      null, 
      null, 
      CurrentWeatherContract.CurrentWeatherEntry._ID + " DESC" 
    ); 
} 

答えて

0

問題は、recylerViewに古いデータをフィードすることです。データを非同期で取得するため、新しいデータがクリックの瞬間に利用できなくなるためです。そのため、新しいデータをデータベースに保存した後で、refreshメソッドを呼び出す必要があります。

0

代わりのrefreshボタンのクリックでadapterを追加し、onclickの側面を、アダプタを設定し、ボタンをクリックするとlistnotifyadapterに更新されます。

List list = getAllItems(); 

recyclerView.setLayoutManager(new LinearLayoutManager(FavouritesActivity.this)); 
mAdapter = new CurrentWeatherAdapter(FavouritesActivity.this, list); 
recyclerView.setAdapter(mAdapter); 

とのonClick()内

list = getAllItem(); 
adapter.notifyDataSetChanged(); 

は、これが役立つことを願っています。

関連する問題