Android I';我试图在我的微调器和带有Viewholder的RecyclerView适配器中的单选按钮之间进行通信
我也完成了我的购物清单项目,但我遇到了一个问题。我需要得到两条信息:Android I';我试图在我的微调器和带有Viewholder的RecyclerView适配器中的单选按钮之间进行通信,android,android-recyclerview,android-room,Android,Android Recyclerview,Android Room,我也完成了我的购物清单项目,但我遇到了一个问题。我需要得到两条信息: 一个bool,看看我的单选按钮是否被选中 我的位置名称的字符串 复杂的是,我想使用片段中的ROOM架构更新SQLite数据库。我无法对数据库执行更新,因为我的片段包含对我的ViewModel的引用,我的适配器包含对我的微调器和单选按钮的引用。当我的recyclerview出现在我的片段中时,我希望能够执行DB函数。我尝试过接口和getter方法,但似乎无法解决这个问题。请帮忙 这是我的卡片布局: <?xml vers
- 一个bool,看看我的单选按钮是否被选中李>
- 我的位置名称的字符串
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:cardCornerRadius="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:id="@+id/textview_groceries_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="20"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:text="Sample Text"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:fontFamily="@font/quicksand_light"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/location_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample location"
app:layout_constraintStart_toStartOf="@id/textview_groceries_item_name"
app:layout_constraintTop_toBottomOf="@id/textview_groceries_item_name" />
<RadioButton
android:id="@+id/radio_full_trolley"
android:layout_width="45dp"
android:layout_height="40dp"
android:background="@drawable/trolley_cart_selector"
android:button="@drawable/full_shopping_cart_small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/spin_shop_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/radio_full_trolley" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
这是我的recyclerview适配器:
public class GroceriesListAdapter extends RecyclerView.Adapter<GroceriesListAdapter.GroceriesItemHolder> {
private List<GroceriesListItem> ingredientsList = new ArrayList<>();
private List<String> shopNames = new ArrayList<>();
private ArrayAdapter<String> shopNameSpinnerAdapter;
@NonNull
@Override
public GroceriesItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//example list to fill spinner, will be changed in the future
shopNames.clear();
shopNames.add("SuperMarket");
shopNames.add("Green Grocer");
shopNames.add("Dollar Shop");
shopNames.add("Asian Market");
shopNames.add("Other");
View groceriesItemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardlayout_groceries_item, parent, false);
shopNameSpinnerAdapter = new ArrayAdapter<>(parent.getContext(), android.R.layout.simple_spinner_item, shopNames);
shopNameSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
return new GroceriesItemHolder(groceriesItemView);
}
@Override
public void onBindViewHolder(@NonNull final GroceriesListAdapter.GroceriesItemHolder holder, int position) {
GroceriesListItem currentGroceryItem = ingredientsList.get(position);
holder.textViewGroceriesItemName.setText(currentGroceryItem.getItemToBuy());
holder.placesSpinner.setAdapter(shopNameSpinnerAdapter);
}
@Override
public int getItemCount() {
return ingredientsList.size();
}
public void setGroceriesList(List<GroceriesListItem> ingredientsList) {
this.ingredientsList = ingredientsList;
notifyDataSetChanged();
}
class GroceriesItemHolder extends RecyclerView.ViewHolder {
private TextView textViewGroceriesItemName;
private Spinner placesSpinner;
private TextView textViewLocation;
private RadioButton trolleyCartStatus;
public GroceriesItemHolder(@NonNull View itemView) {
super(itemView);
textViewGroceriesItemName = itemView.findViewById(R.id.textview_groceries_item_name);
**placesSpinner = itemView.findViewById(R.id.spin_shop_name);**
textViewLocation = itemView.findViewById(R.id.location_textView);
**trolleyCartStatus = itemView.findViewById(R.id.radio_full_trolley);**
}
}
//this is for deleting the item in the fragment -- completed
public GroceriesListItem getListItemAt(int position) {
return ingredientsList.get(position);
}
}
公共类GroceriesListAdapter扩展了RecyclerView.Adapter{
private List ingredientsList=new ArrayList();
private List shopNames=new ArrayList();
private ArrayAdapter shopNameSpinnerAdapter;
@非空
@凌驾
public GroceriesItemHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
//填充微调器的示例列表将在将来更改
shopNames.clear();
店名。添加(“超市”);
店名。添加(“绿色食品商”);
店名。添加(“美元店”);
店名。添加(“亚洲市场”);
店名。添加(“其他”);
View groceriesItemView=LayoutFlater.from(parent.getContext()).flate(R.layout.cardlayout\u groceries\u项目,parent,false);
shopNameSpinnerAdapter=新的ArrayAdapter(parent.getContext(),android.R.layout.simple\u spinner\u项目,shopNames);
shopNameSpinnerAdapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
返回新的GroceriesItemHolder(groceriesItemView);
}
@凌驾
BindViewHolder上的公共无效(@NonNull final GroceriesListAdapter.GroceriesItemHolder,int位置){
杂货清单项目currentGroceryItem=IngCreditsList.get(位置);
holder.textViewGroceriesItemName.setText(currentGroceryItem.getItemToBuy());
holder.placesSpinner.setAdapter(shopNameSpinnerAdapter);
}
@凌驾
public int getItemCount(){
返回IngreditsList.size();
}
公共作废设置杂货清单(清单InCreditsList){
this.ingredientsList=ingredientsList;
notifyDataSetChanged();
}
类GroceriesItemHolder扩展了RecyclerView.ViewHolder{
私有文本视图文本视图杂货项目名称;
私人纺纱厂;
私有文本视图文本视图位置;
私人无线按钮无轨电车状态;
公共杂货项目持有人(@NonNull View itemView){
超级(项目视图);
textViewGroceriesItemName=itemView.findViewById(R.id.textview\u groceries\u item\u name);
**placesSpinner=itemView.findviewbyd(R.id.spin\u店铺名称)**
textViewLocation=itemView.findViewById(R.id.location\u textView);
**电车状态=itemView.findViewById(R.id.radio\u full\u电车)**
}
}
//这是为了删除片段中的项--已完成
公共食品清单项目getListItemAt(内部位置){
返回InCreditsList.get(位置);
}
}
这是我的片段:
public class GroceriesListFragment extends Fragment {
private ScheduleViewModel scheduleViewModel;
private GroceriesListViewModel groceriesListViewModel;
private RecyclerView groceriesRecyclerView;
private List<String> listOfCurrentMeals = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final GroceriesListAdapter groceriesListAdapter = new GroceriesListAdapter();
groceriesListViewModel = new ViewModelProvider(getActivity()).get(GroceriesListViewModel.class);
scheduleViewModel = new ViewModelProvider(getActivity()).get(ScheduleViewModel.class);
scheduleViewModel.getMealSchedule().observe(getViewLifecycleOwner(), new Observer<List<MealSchedule>>() {
@Override
public void onChanged(List<MealSchedule> currentMealSchedule) {
//get all meals into a list and remove Not decided entries -- completed
for (MealSchedule oneDaysMeals : currentMealSchedule) {
String breakfastDish = oneDaysMeals.getBreakfastMeal();
String LunchDish = oneDaysMeals.getLunchMeal();
String DinnerDish = oneDaysMeals.getDinnerMeal();
if (breakfastDish.equals("Not Decided")) {
continue;
} else {
listOfCurrentMeals.add(breakfastDish);
}
if (LunchDish.equals("Not Decided")) {
continue;
} else {
listOfCurrentMeals.add(LunchDish);
}
if (DinnerDish.equals("Not Decided")) {
continue;
} else {
listOfCurrentMeals.add(DinnerDish);
}
}
//get current Groceries list and refresh list if changed -- completed
if (listOfCurrentMeals.isEmpty()) {
Toast.makeText(getContext(), "No items to show", Toast.LENGTH_SHORT).show();
} else {
groceriesListViewModel.clearList();
for (String dishNames : listOfCurrentMeals) {
scheduleViewModel.getOneDishItem(dishNames).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new DisposableMaybeObserver<Dish>() {
@Override
public void onSuccess(Dish dish) {
groceriesListViewModel.insertIngredient(new GroceriesListItem(dish.getIngredient1(), false, ""));
if (!dish.getIngredient2().isEmpty()) {
groceriesListViewModel.insertIngredient(new GroceriesListItem(dish.getIngredient2(), false, ""));
}
if (!dish.getIngredient3().isEmpty()) {
groceriesListViewModel.insertIngredient(new GroceriesListItem(dish.getIngredient3(), false, ""));
}
if (!dish.getIngredient4().isEmpty()) {
groceriesListViewModel.insertIngredient(new GroceriesListItem(dish.getIngredient4(), false, ""));
}
if (!dish.getIngredient5().isEmpty()) {
groceriesListViewModel.insertIngredient(new GroceriesListItem(dish.getIngredient5(), false, ""));
}
if (!dish.getIngredient6().isEmpty()) {
groceriesListViewModel.insertIngredient(new GroceriesListItem(dish.getIngredient6(), false, ""));
}
if (!dish.getIngredient7().isEmpty()) {
groceriesListViewModel.insertIngredient(new GroceriesListItem(dish.getIngredient7(), false, ""));
}
if (!dish.getIngredient8().isEmpty()) {
groceriesListViewModel.insertIngredient(new GroceriesListItem(dish.getIngredient8(), false, ""));
}
if (!dish.getIngredient9().isEmpty()) {
groceriesListViewModel.insertIngredient(new GroceriesListItem(dish.getIngredient9(), false, ""));
}
if (!dish.getIngredient10().isEmpty()) {
groceriesListViewModel.insertIngredient(new GroceriesListItem(dish.getIngredient10(), false, ""));
}
}
@Override
public void onError(Throwable e) {
Toast.makeText(getContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete() {
}
});
}
}
}
});
//get list of ingredients and observe changes
groceriesListViewModel.getCurrentList().observe(getViewLifecycleOwner(), new Observer<List<GroceriesListItem>>() {
@Override
public void onChanged(final List<GroceriesListItem> groceriesListItems) {
groceriesListAdapter.setGroceriesList(groceriesListItems);
}
});
View groceriesListFragmentView = inflater.inflate(R.layout.frag_groceries_list, container, false);
groceriesRecyclerView = groceriesListFragmentView.findViewById(R.id.groceries_list_recyclerview);
groceriesRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
groceriesRecyclerView.setAdapter(groceriesListAdapter);
//swipe to delete function -- completed
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
groceriesListViewModel.deleteIngredient(groceriesListAdapter.getListItemAt(viewHolder.getAdapterPosition()));
}
}).attachToRecyclerView(groceriesRecyclerView);
//clear list function -- completed
final Button clearListButton = groceriesListFragmentView.findViewById(R.id.clear_list_button);
clearListButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
groceriesListViewModel.clearList();
}
});
//how to create a method with reference to spinner and radio button?
return groceriesListFragmentView;
}
公共类GroceriesListFragment扩展了片段{
私有ScheduleViewModel ScheduleViewModel;
私人杂货列表视图模型杂货列表视图模型;
私人垃圾回收站;
private List OfCurrentFounds=new ArrayList();
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
final GroceriesListAdapter GroceriesListAdapter=新的GroceriesListAdapter();
groceriesListViewModel=新的ViewModelProvider(getActivity()).get(groceriesListViewModel.class);
scheduleViewModel=新的ViewModelProvider(getActivity()).get(scheduleViewModel.class);
scheduleViewModel.getMealSchedule().observe(getViewLifecycleOwner(),new Observer()){
@凌驾
更改后的公共无效(列表currentMealSchedule){
//将所有膳食放入列表并删除未决定的条目--已完成
对于(测量时间表一天表:当前测量时间表){
String breakfastDish=oneDaysMeals.getBreakfastDine();
String午餐盘=oneDaysMeals.get午餐();
String DinnerDish=oneDaysMeals.getDinnerMeal();
如果(早餐盘等于(“未决定”)){
继续;
}否则{
当前膳食清单。添加(早餐菜);
}
如果(午餐盘等于(“未决定”)){
继续;
}否则{
当前膳食列表。添加(午餐菜);
}
如果(晚餐时间等于(“未决定”)){
继续;
}否则{
当前膳食列表。添加(晚餐餐);
}
}
//获取当前杂货清单并刷新清单(如果已更改)——已完成
if(listofCurrentFounds.isEmpty()){
Toast.makeText(getContext(),“没有要显示的项目”,Toast.LENGTH_SHORT.show();
}否则{
groceriesListViewModel.clearList();
for(字符串dishNames:ListofCurrentFounds){
scheduleViewModel.getOneDishItem(dishNames).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(新的可处置的MaybeObserver()){
@凌驾
成功时的公共空间(盘){
groceriesListViewModel.InsertingCredit(新的GroceriesListItem(dish.GetingCredit1(),false,“”);