适配器和片段发生了我无法理解的事情 [英] Adapter and Fragment Something incomprehensible to me happens
问题描述
问题是,当我将类别ID从适配器发送到MainFragment中的getDishes时,即使在getDishes
中CategoryId值发生了变化,我也无法从屏幕上的Dishes中看到任何内容,我希望代码能阐明事项
谢谢
The problem is when I send the CategoryId from the adapter to the getDishes in the MainFragment, I cannot see anything from the Dishes on the screen even though the CategoryId value changes within the getDishes I hope the code clarifies the matter Thank you
在MainFragment内部
Inside MainFragment
public class MainFragment extends Fragment
{
public void getCategory()
{
categoryViewModel.getAll().observe(this, new Observer<List<Category>>() {
@Override
public void onChanged(List<Category> categories) {
MainFragment.this.categories= categories;
categoryAdapter.SetData(categories);
if(categories.size() != 0)
{
selectedCategoryID = categories.get(0).getId();
getDishes(selectedCategoryID);
}
else
{
dishes = new ArrayList<>();
mainFragmentDishesAdapter.sSetData(dishes);
}
}
});
}
我设置了一个Toast来查看CategoryId值是否正在更改,并且是否正在更改
I set a Toast to see if the CategoryId value is changing or not and it is
public void getDishes(final int CategoryIdd)
{
selectedCategoryID = CategoryIdd;
dishesViewModel.getAll(CategoryIdd).observe(this, new Observer<List<Dishes>>() {
@Override
public void onChanged(List<Dishes> dishes)
{
if(dishes.size() == 0)
MainFragment.this.dishes = dishes;
mainFragmentDishesAdapter.sSetData(dishes);
Toast.makeText(getActivity(), ""+CategoryIdd, Toast.LENGTH_SHORT).show();
}
});
}
在适配器内部
public class MainFragmentCategoryAdapter extends RecyclerView.Adapter<MainFragmentCategoryVH>
{
public CallDishesBack mCallDishesBack;
public interface CallDishesBack { void getDishes(int CategoryIdd);}
mCallDishesBack = (CallDishesBack) context;
holder.getView().setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick(View view)
{
mCallDishesBack.getDishes(category.getId());
}
});
public void SetData(List<Category> categories)
{
this.categories = categories;
notifyDataSetChanged();
}
在接口的MainActivity内部
Inside the MainActivity for the interface
public class MainActivity extends AppCompatActivity implements MainFragmentCategoryAdapter.CallDishesBack
{
MainFragment mainFragment;
if(savedInstanceState == null){
mainFragment = MainFragment.newInstance(new Bundle()); // use real bundle here
getSupportFragmentManager()
.beginTransaction()
.add(R.id.content, mainFragment, "FragMain").commit();
}
public void getDishes(int CategoryIdd) {
if(mainFragment != null)
{
mainFragment.getDishes(CategoryIdd);
}
}
仅出现第一个类别的菜肴
Only the dishes for the first Category appear
如果不是这样,我希望问题很清楚,我在这里,非常感谢
I hope that the problem is clear if it is not like that, I am here and thank you very much
推荐答案
使用接口
是我的错误,我与BottomSheet或其他任何接口一起使用。我不知道还有使用界面
的另一种方法,旧方法效果不佳
It was my mistake when I used the Interface The one that I use with BottomSheet or anything else. I did not know that there is another way to use the interface The old method was not working well
适配器的第一步
public CallDishesYes mCallDishesYes;
public MainFragmentCategoryAdapter(Context context, List<Category> categories,CallDishesYes mCallDishesYes) {
this.context = context;
this.categories = categories;
this.mCallDishesYes = mCallDishesYes;
}
holder.getView().setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick(View view)
{
///region Old solution that didn't work
//mCallDishesBack.getDishes(category.getId());
///endregion
mCallDishesYes.getDishes(category.getId());
}
});
public interface CallDishesYes {
void getDishes(int IdCategory);
}
分段拖曳
public class MainFragment extends Fragment implements MainFragmentCategoryAdapter.CallDishesYes
{
dishes = new ArrayList<>();
mainFragmentDishesAdapter = new MainFragmentDishesAdapter(getActivity(),dishes);
public void getDishes(int CategoryIdd)
{
dishesViewModel.getAll(CategoryIdd).observe(this, new Observer<List<Dishes>>() {
@Override
public void onChanged(List<Dishes> dishes)
{
if (dishes.size() == 0)
MainFragment.this.dishes = dishes;
mainFragmentDishesAdapter.sSetDate(dishes);
}
});
}
这篇关于适配器和片段发生了我无法理解的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!