gpt4 book ai didi

java - 将数据从 RecyclerView.Adapter 传递到 fragment onClick

转载 作者:太空宇宙 更新时间:2023-11-04 09:46:10 25 4
gpt4 key购买 nike

我试图弄清楚如何将数据从 RecyclerView 中单击的项目获取到 Fragment 中的 ListView 。我似乎不知道如何做到这一点,因为我无法让我的 bundle 工作。

我尝试过 Stackoverflow 上提供的各种解决方案,但没有一个有效。我已设法从 recyclerview 获取信息,但我一直在试图弄清楚如何将其传递给 fragment 。有人可以帮我吗?

适配器类:

public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.ViewHolder> {

private Context context;

ArrayList<FoodActivity> list;
public FoodAdapter(ArrayList<FoodActivity> list){
this.list = list;
}


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item_food, parent, false);

return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.foods.setText(list.get(position).getName());
holder.carbo.setText(list.get(position).getCarbohydrates());
holder.protein.setText(list.get(position).getProtein());
holder.fats.setText(list.get(position).getFats());

//Get items from recyclerview when user clicks them. Then send them to FoodFragment
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String foods = list.get(position).getName();
String carbo = list.get(position).getCarbohydrates();
String protein = list.get(position).getProtein();
String fats = list.get(position).getFats();

Toast.makeText(v.getContext(), "test: " + foods, Toast.LENGTH_SHORT).show();


}
});
}


@Override
public int getItemCount() {
return list.size();
}

class ViewHolder extends RecyclerView.ViewHolder {
TextView foods, carbo, fats, protein;

public ViewHolder(View itemView) {
super(itemView);

carbo = itemView.findViewById(R.id.carbo);
protein = itemView.findViewById(R.id.protein);
fats = itemView.findViewById(R.id.fats);
foods = itemView.findViewById(R.id.food);
}
}
}

数据来源的 fragment :

public class TrackingFragment extends Fragment {

DatabaseReference databaseReference;
ArrayList<FoodActivity> list;
RecyclerView recyclerView;
SearchView searchView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tracking, container, false);

databaseReference = FirebaseDatabase.getInstance().getReference().child("foods");
recyclerView = view.findViewById(R.id.rv);
searchView = view.findViewById(R.id.searchFood);

return view;
}

@Override
public void onStart() {
super.onStart();

if(databaseReference != null){
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
list = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()){
list.add(ds.getValue(FoodActivity.class));
}
FoodAdapter adapter = new FoodAdapter(list);
recyclerView.setAdapter(adapter);


}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
if(searchView != null){
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
search(newText);
return true;
}
});
}
}

private void search(String str){
ArrayList<FoodActivity> searchList = new ArrayList<>();
for(FoodActivity object : list){
if(object.getName().toLowerCase().contains(str.toLowerCase())){
searchList.add(object);
}
}
FoodAdapter foodAdapter = new FoodAdapter(searchList);
recyclerView.setAdapter(foodAdapter);
}

}

以及它需要去的类:

public class FoodFragment extends Fragment {

ImageButton addFood;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_food, container, false);

addFood = view.findViewById(R.id.addFood);

addFood.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.fragment_container, new TrackingFragment());
fr.addToBackStack(null).commit();
}
});

return view;
}

}

最佳答案

最简单的方法是向 Activity 添加所需的变量,使用 onClick() 设置它们的值,然后检索其他 fragment 中的数据:

  • Activity 中:

    String foods;

    public String getFoods() {
    return foods;
    }

    public void setFoods(String foods) {
    this.foods = foods;
    }
  • 在适配器中:

       holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    ((YourActivity)getActivity()).setFoods("Any value");
    }
    });
  • 在目标 fragment 中:

    字符串食物 = ((YourActivity)getActivity()).getFoods();

关于java - 将数据从 RecyclerView.Adapter 传递到 fragment onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55405312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com