0

I have weird glitch that I have hard time explaining with words. So a recorded this video to show it. FAB opens a activity which has a EditText and a confirm button. When you press the confirm button the name that written to EditText will be added to a RecyclerView in Library fragment. But if you don't write anything to EditText and press confirm it will add "Johan". But here is the thing, if you didn't even click to EditText it will add Johan to ArrayList but it won't show it until you add another name "properly" or restart the Players activity.

Library Fragment:

public class SetupplayerLibraryFragment extends Fragment {

private static String TAG = "SetupplayerLibraryFragment";
static View view;

InternalOperator IO;

RecyclerView libraryList;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager manager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    view = inflater.inflate(R.layout.fragment_setupplayer_library, container, false);

    libraryList = view.findViewById(R.id.libraryList);
    libraryList.addItemDecoration(new DividerItemDecoration(libraryList.getContext(), DividerItemDecoration.VERTICAL));

    manager = new LinearLayoutManager(getActivity());
    adapter = new PlayerAdapter(IO.players);
    libraryList.setLayoutManager(manager);
    libraryList.setAdapter(adapter);

    return view;
}
}

New Player Activity:

public class SetupplayerAddActivity extends AppCompatActivity {

private static String TAG = "SetupplayerAddActivity";

EditText nameText;
Button confirmButton;
Toolbar toolbar;

InternalOperator IO;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setupplayer_add);

    nameText = findViewById(R.id.text_add_name);
    confirmButton = findViewById(R.id.button_add_confirm);


    confirmButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (nameText.getText().length() != 0) {
                if (ContextCompat.checkSelfPermission(SetupplayerAddActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                    nameText.clearComposingText();
                    ExternalOperator.savePlayers(nameText.getText().toString());
                    IO.players.add(new Player(nameText.getText().toString(), 0));
                    finish();
                } else {
                    Toast.makeText(SetupplayerAddActivity.this, "Permission to use storage is needed for this action!", Toast.LENGTH_SHORT).show();
                }
            } else {
                if (ContextCompat.checkSelfPermission(SetupplayerAddActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                    ExternalOperator.savePlayers("Johan");
                    IO.players.add(new Player("Johan", 0));
                    finish();//Activityi kapıyor
                } else {
                    Toast.makeText(SetupplayerAddActivity.this, "You are really the Johan! Radiance save us!", Toast.LENGTH_SHORT).show();
                }
            }
        }
    });

}

}

I removed some of the useless codes to make it more clear.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Brogolem35
  • 101
  • 12

1 Answers1

1

You have to call RecyclerView.Adapter.notifyDataSetChanged() everytime you add or delete or update the list that populates RecyclerView to make it aware of the changes.

For Example

IO.players.add(new Player(nameText.getText().toString(), 0));
adapter.notifyDataSetChanged().
touhid udoy
  • 4,005
  • 2
  • 18
  • 31
  • It worked! But I have somethings to say. 1- Why it wasn't working when I didn't clicked? 2- The full code is libraryList.getAdapter().notifyDataSetChanged(), so I will edit your answer and add it. 3- I added the code to onResume method of library fragment, will it cause a problem? – Brogolem35 Aug 11 '19 at 06:38
  • @Brogolem35 1.recycler view doesn't update view until it is necessary. so its your job if something changes, you have to inform the adapter of the changed. it is how this is designed. you can add this on resume, but, it is better to add using interface and listeners call back. you can check here https://stackoverflow.com/questions/994840/how-to-create-our-own-listener-interface-in-android/18585247#18585247 – touhid udoy Aug 11 '19 at 08:07