Recyclerview 在实现 list.clear() 和 adapter.notitfyDataSetChange



我正在从远程数据库填充回收器视图。

回收器视图位于片段内。

首次加载片段时,回收器视图将显示所有项目。

当用户单击某个项目时,将加载另一个片段。

然后,如果用户单击后退按钮,回收器视图将显示所有项目,但现在显示两次。在最后一项之后,将再次显示所有项目。

我在从远程数据库加载数据的函数中包含以下两行:

    marcas.clear();// public List<Marca> marcas;
    adapter.notifyDataSetChanged();// private MarcaAdapter adapter;

但我做错了吗?

编辑

在创建中

   /**
         * Showing Swipe Refresh animation on activity create
         * As animation won't start on onCreate, post runnable is used
         */
        mSwipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(true);
                marcas.clear();
                adapter.notifyDataSetChanged();

            }
        });
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        marcas = new ArrayList<>();

        gridLayout = new GridLayoutManager(getActivity(), 2);
        recyclerView.setLayoutManager(gridLayout);
        adapter = new MarcaAdapter(getActivity(), marcas);

        recyclerView.setAdapter(adapter);
      //  marcas.clear();

        getDirectoriosFromDB(0);//LLAMADA 1

要加载数据:

private void getDirectoriosFromDB(int id) {
 marcas.clear();
 adapter.notifyDataSetChanged();
    AsyncTask<Integer, Void, Void> asyncTask = new AsyncTask<Integer, Void, Void>() {
        @Override
        protected Void doInBackground(Integer... addressesIds) {
          //  Log.d("HOLA PERFIL", "UID REGISTRADO ANTES DE CARGAR REECYCLER: " + user_id);

            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("https://...marcas_todas.php")
                    .build();
            try {
                Log.d("HOLA ADDRESSES", "DIRECCION LEIDA:cargando datos en getdirectorios INICIO ARRAY" );
                okhttp3.Response response = client.newCall(request).execute();
                JSONArray array = new JSONArray(response.body().string());
                for (int i = 0; i < array.length(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    Log.d("HOLA ADDRESSES", "DIRECCION LEIDA:cargando datos en getdirectorios " +array);
                    Log.d("HOLA ADDRESSES", "DIRECCION LEIDA: " + i);
                    Marca marca = new Marca(object.getInt("id_car_make"),
                            object.getString("name")
                            );
                    marcas.add(marca);
                }

            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {

            adapter.notifyDataSetChanged();
            mSwipeRefreshLayout.setRefreshing(false);
        }
    };
    asyncTask.execute(id);
}

基本上您的列表在活动内部是全局的,但在适配器中不是,因此当您刷新列表时,适配器已填充 arraylist 实例,因此这会产生问题。 刷新列表后应重新填充适配器: 像这样:

mSwipeRefreshLayout.post(new Runnable() {
        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(true);
            marcas.clear();
            adapter = new MarcaAdapter(getActivity(), marcas);
            adapter.notifyDataSetChanged();

        }
    });

最新更新