0

This is the code of my fragment, when i created a recyclerview, when i can see a list if I click on I can see detail in antoher page, but when I come back I can't see the list.

@Override

public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    SharedPreferences pref = getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);


    adapter = new PlatformAdapter(data);
    recyclerView = view.findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    //recyclerView.setAdapter(adapter);

    if (pref.getBoolean("firstTime", true)) {

        RequestVolley.getInstance(getContext())
                .doGetRequest("http://www....json", new RequestVolley.OnCompleteCallback() {
                    @Override
                    public void onCompleted(String response) {

                        try {
                            JSONArray jsonArray = new JSONArray(response);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject object = jsonArray.optJSONObject(i);
                                if (object != null) {
                                    data.add(Platform.parseJSON(object));
                                }
                            }
                            adapter.notifyDataSetChanged();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

        new Thread(new Runnable() {
            @Override
            public void run() {
                DB.getInstance(getContext()).getPlatformDao().insert(data);

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });


                SharedPreferences.Editor editor = pref.edit();
                editor.putBoolean("firstTime", false);
                editor.apply();

                final PlatformAdapter adapter = new PlatformAdapter(data);

                getActivity().runOnUiThread(new Runnable() {
                   @Override
                    public void run() {
                        recyclerView.setAdapter(adapter);
                    }
                });
            }
        }).start();

    }
    if(pref.getBoolean("firstTime",false)){
        new Thread(new Runnable() {
            @Override
            public void run() {
                data.addAll(DB.getInstance(getContext()).getPlatformDao().findAll());

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });

                        final PlatformAdapter adapter = new PlatformAdapter(data);
                        recyclerView.post(new Runnable() {
                            @Override
                            public void run() {
                                recyclerView.setAdapter(adapter);
                            }
                        });
            }

        }).start();
    }

} }

this is the code of my app in the point of inserto or reload from roomDatabase, the first time I oper the page it's ok, but the second time I can see only a white page.Why? I dont'have any error. Why it isn't on the DB, or it isn't reload?

This is my Dao

@Dao

public interface PlatformDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
public void insertOne(Platform platform);

@Insert(onConflict=OnConflictStrategy.REPLACE)
public void  insert(List<Platform> platforms);

@Insert(onConflict=OnConflictStrategy.REPLACE)
public void insertVarArgs(Platform...platforms);

@Update
public void update(Platform platform);

@Delete
public void delete(Platform platform);

@Query("SELECT * FROM platforms")
public List<Platform> findAll();

@Query("SELECT * FROM platforms WHERE cdenominazione__=:cdenominazione__")
public Platform findById(String cdenominazione__);

}

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Martak39
  • 13
  • 2

1 Answers1

0

When you add the fragment are you doing an add or a replace? Aso are you adding the fragment to the backstack?

Vinnie
  • 252
  • 1
  • 9
  • I navigate with an action at the second fragment, and then I come back to the first and I can't see anything. But when I do the insert or the load from DB it doesn't work, I think. – Martak39 Sep 22 '21 at 14:29