我有奇怪的小故障,我很难用言语解释。因此,有一个录制的this video来显示它。 FAB将打开一个具有EditText和确认按钮的活动。当您按下确认按钮时,写入EditText的名称将被添加到RecyclerView的Library片段中。但是,如果您不向EditText写任何东西,然后按确认,它将添加“ Johan”。但这就是问题,如果您什至没有单击EditText,它将把Johan添加到ArrayList中,但是直到您“适当地”添加另一个名称或重新启动Players活动之前,它才显示出来。
库片段:
android.util.ArrayMap
新玩家活动:
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;
}
}
我删除了一些无用的代码以使其更加清晰。
答案 0 :(得分:1)
每次添加或删除或更新填充RecyclerView的列表时,都必须调用RecyclerView.Adapter.notifyDataSetChanged()
,以使其了解更改。
例如
IO.players.add(new Player(nameText.getText().toString(), 0));
adapter.notifyDataSetChanged().