当前位置: 首页 > 知识库问答 >
问题:

如何将数据从BaseAdapter传递到AppCompatDialogFragment?Android

唐钊
2023-03-14

我正在尝试从我的FoodAdapter中传递数据,该数据从BaseAdapter扩展到从AppCompatDialog片段扩展的FoodDialog。当用户单击list_item中的按钮时,我需要将数据传递给FoodDialog。

addToCart = convertView.findViewById(R.id.button);
        View finalConvertView = convertView;
        addToCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", String.valueOf(position));
                TextView s = (TextView) finalConvertView.findViewById(R.id.foodName);
                ConfirmFoodDialog confirmFoodDialog = new ConfirmFoodDialog();

                confirmFoodDialog.show(((CategoryMenu) context).getSupportFragmentManager(), "Title");

                Intent intent = new Intent(finalConvertView.getContext(), ConfirmFoodDialog.class);
                intent.putExtra("Dish", s.getText().toString()); // s.getText().toString() returns the desired value
                
                
            }

确认FoodDialog。班

public Dialog onCreateDialog(Bundle savedInstanceState){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyCustomTheme);
        LayoutInflater inflater = getActivity().getLayoutInflater();

        Intent intent = getActivity().getIntent();
        food = intent.getStringExtra("Dish"); // food is a String variable
        System.out.println(food); // I don't know why food returns null
}

共有1个答案

钱德海
2023-03-14

鉴于您正在使用show(FragmentManager)显示对话框,您的对话框似乎是DialogFragment的子类。如果是这种情况,则将信息传递给它的方式与将信息传递给任何片段的方式相同:通过参数束。

Bundle args = new Bundle();
args.putString("Dish", s.getText().toString());

ConfirmFoodDialog confirmFoodDialog = new ConfirmFoodDialog();
confirmFoodDialog.setArguments(args);
confirmFoodDialog.show(((CategoryMenu) context).getSupportFragmentManager(), "Title");

要从片段中读出值,请访问参数并读取您的信息:

public Dialog onCreateDialog(Bundle savedInstanceState){
    // ...

    food = getArguments().getString("Dish");
    System.out.println(food);
}

您可以通过为Dialog片段实现newInstance()模式来简化第一部分。

 类似资料:
  • 我创建了一个新的电子应用程序。 在索引中。jsi使用节点文件系统加载数据 如果我试着使用require。JSIT之所以能够工作,是因为它运行在不同的线程上,而不是使用节点进行初始化,更像是一个实际的浏览器窗口。但有没有办法从索引中传递数据呢。js到main。js 我不知道我对这个问题的看法是否部分正确 如果您需要更多代码或信息,请询问!

  • 问题内容: 我需要在单击recyclerview的图像时将数据从传递 到 。有人可以帮忙吗? 问题答案: 创建一个侦听器接口,然后让您的MainActivity实现它。这样,您可以在onClick方法中调用回调方法。 接口: 主要活动: 您的VideoAdapter:

  • 问题内容: 我有一个一个的按钮在我的课,这是我的主要窗口类: 我有一个类就是主窗口的内容。 如何访问内容中的此按钮?有没有更好的方法来组织和控制器以促进此访问? 问题答案: 这样使用委托怎么样?本示例将更改按钮的标题。

  • 我正试图将数据从Kafka传递到火花流。 这就是我到现在所做的: null

  • 在控制器中(也可能是Main或另一个文件,我不确定这一切是如何工作的),我们有以下内容: 在Scene Bilder生成的FXML中,如下所示:

  • 问题内容: 我正在尝试使用Django和D3.js编写非常基本的条形图。我有一个名为play的对象,其中datetime字段称为date。我想做的是显示按月分组的播放次数。基本上我有两个问题: 我如何按月份将这些分组,并计算当月的播放次数 将这些信息从Django转换为D3可用的最佳方法是什么。 现在,我在这里查看了其他答案,并尝试了 这接近于我想要的信息,但是当我尝试将其输出到模板中时,它在月末