Я новичок в Android ViewModel. Я пытался добавить элемент arraylist, используя viewmodel и recyclerview, но я не смог. Пожалуйста, помогите мне ..
Спасибо заранее.
Этоa GalaryViewModel:
class GalleryViewModel extends ViewModel {
private MutableLiveData<ArrayList<String>>fruits ;
MutableLiveData<ArrayList<String>> getFruits() {
if(fruits == null){
fruits = new MutableLiveData<>();
loadFruits();
}
return fruits;
}
private void loadFruits(){
ArrayList<String> fruitList = new ArrayList<>();
fruitList.add("Apple");
fruitList.add("Banana");
fruitList.add("Pinaple");
fruitList.add("Apple");
fruitList.add("Cucamber");
fruitList.add("Cocunut");
fruits.setValue(fruitList);
}
}
Это GalleryFragment: где я не мог добавить значение setText внутри public void onChanged () Function
public class GalleryFragment extends Fragment {
private Context context;
private View view;
private GalleryViewModel galleryViewModel;
private ArrayList<String> fruitList;
public GalleryFragment(Context context, View view, GalleryViewModel galleryViewModel, ArrayList<String> fruitList) {
this.context = context;
this.view = view;
this.galleryViewModel = galleryViewModel;
this.fruitList = fruitList;
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
RecyclerView recyclerView = view.findViewById(R.id.gRecyclerView);
final GallaryRecyclerView adapter = new GallaryRecyclerView(context,fruitList);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this.context));
galleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
final TextView textView = root.findViewById(R.id.text_gallery);
galleryViewModel.getFruits().observe(this, new Observer<ArrayList<String>>() {
@Override
public void onChanged(ArrayList<String> strings) {
textView.setText((CharSequence) strings);
}
// @Override
// public void onChanged(@Nullable String s) {
// textView.setText(s);
// }
});
return root;
}
}
Это GalleryRecyclerview:
public class GallaryRecyclerView extends RecyclerView.Adapter<GallaryRecyclerView.ViewHolder>{
private Context context;
private ArrayList<String> fruitList;
public GallaryRecyclerView(Context context, ArrayList<String> fruitList) {
this.context = context;
this.fruitList = fruitList;
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView fruitsView;
ViewHolder(@NonNull View itemView) {
super(itemView);
fruitsView = itemView.findViewById(R.id.text_gallery);
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.fragment_gallery,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.fruitsView.setText(fruitList.get(position));
}
@Override
public int getItemCount() {
return fruitList.size();
}
}