【问题标题】:RecyclerView updating on second button instead of firstRecyclerView 在第二个按钮而不是第一个按钮上更新
【发布时间】:2017-12-10 17:56:48
【问题描述】:

我有一个应用程序,它将天气信息存储在 SQLite 数据库中并将其显示在 RecyclerView 中。我正在尝试实现一个函数来更新数据库并使用表中的更新内容刷新 RecyclerView。

当用户单击“刷新”按钮时,我已经实现了这一点。但是,RecyclerView 仅在第二次单击按钮时更新,我无法弄清楚为什么它在第一次单击时没有更新。

下面的类是一个异步任务,它从 API 获取天气数据并成功解析。 “onPostExecute”语句是我更新数据库值的地方。

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

我的点击监听器:

    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() 方法:

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

【问题讨论】:

    标签: android sqlite android-recyclerview


    【解决方案1】:

    问题是您将旧数据输入到 recylerView 中,因为您异步检索数据,新数据在单击时将不可用。因此,您应该在将新数据保存到数据库后调用 refresh 方法。

    【讨论】:

      【解决方案2】:

      不要在单击refresh 按钮时添加adapter,而是将适配器设置在onclick 之外,并在单击按钮时更新listnotify adapter

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

      在 onClick() 内部

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

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2011-07-13
        • 2020-10-28
        • 1970-01-01
        • 1970-01-01
        • 2014-04-19
        • 2017-08-06
        • 1970-01-01
        • 2015-09-30
        • 2023-03-22
        相关资源
        最近更新 更多