okButton을 클릭할 때 마다, 새로운 아이템(데이터)들이 RecyclerView로 차례로 저장되어야하는데 처음에 저장한 아이템만 보여집니다... 몇 날 며칠을 고민했는데도 도저히 답이 안나오네요 고수님들 답변 부탁드립니다ㅠㅠ
1. okButton 이 있는 코드
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String recordtitle = recordTitle.getText().toString();
recordObject saveRecordItems= new recordObject("2020/10/10",recordtitle,"5km"," 3:55");
itemsList.add(saveRecordItems);
Toast.makeText(context, "기록을 저장했습니다.", Toast.LENGTH_SHORT).show();
// Log.d("list", String.valueOf(itemList.size()));
// 커스텀 다이얼로그를 종료한다.
mydlg.dismiss();
}
});
2. Adapter 부분 (viewHolder 는 생략했습니다)
public class RecordAdapter extends RecyclerView.Adapter<RecordAdapter.ViewHolder> {
ArrayList<recordObject> items;
public RecordAdapter(ArrayList<recordObject> list){
this.items = list;
}
public void add(recordObject item) {
items.add(item);
// notifyDataSetChanged();
}
@NonNull
@Override
//화면 최초 로딩하고, xml파일 inflate+ViewHolder 생성
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.record_item, parent, false);
return new RecordAdapter.ViewHolder(itemView);
}
@Override
public int getItemCount() {
return items.size();
}
public ArrayList<recordObject> getListData(){
return items;
}
public void setListData(ArrayList<recordObject> listData){
this.items= listData;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
recordObject item = items.get(position);
holder.setItem(item);
}
3. Recyclerview 가 차례로 나타나야 하는 곳!!
public class recordFragment extends Fragment {
public static ArrayList<recordObject> itemsList = new ArrayList<>();
public static RecordAdapter adapter;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_record, container, false);
RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
if(adapter == null){
adapter= new RecordAdapter(itemsList);
}
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
recyclerView.setHasFixedSize(true);
return view;
}