【问题标题】:Pass String and Integer from Activity to Fragment将字符串和整数从 Activity 传递到 Fragment
【发布时间】:2021-08-31 09:39:14
【问题描述】:

我的应用程序有问题,您能给我解决方案吗? 我需要将字符串和整数(sName 和 sPrice)从 MainActivity 发送到 CartFragment,然后我将填充字符串和整数进入列表视图。像这样的编码:

MainActivity.java

ArrayList<String> sName = new ArrayList<>();
ArrayList<Integer> sPrice = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = findViewById(R.id.shop_list);

    ProductView();

    MyAdapter adapter = new MyAdapter(this, sName, sPrice);

    listView.setAdapter(adapter);
    listView.setOnItemClickListener((parent, view, position, id) -> {
        if(position == 0) {
             ?
        }
        if(position == 1) {
             ?

        }
        if(position == 2) {
             ?

        }
        if(position == 3) {
             ?
        }
        if(position == 4) {
             ?
        }
        Toast.makeText(MainActivity.this, "Ditambahkan ke keranjang", Toast.LENGTH_SHORT).show();
    });
}

private void ProductView() {
    sName.add("Cabai");
    sPrice.add(5000);
    sName.add("Tomat");
    sPrice.add(3000);
    sName.add("Terong");
    sPrice.add(6000);
    sName.add("Wortel");
    sPrice.add(5000);
    sName.add("Kecambah");
    sPrice.add(4000);
}

这是 CartFragment.java

ArrayList<String> cart_name = new ArrayList<>();
ArrayList<Integer> cart_price = new ArrayList<>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    ?????????????????????????????????????

    ListView listView = view.findViewById(R.id.cart_list);

    Adapter adapter = new Adapter(getActivity(), cart_name, cart_price);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener((parent, v, position, id) ->
            Toast.makeText(getActivity(), cart_name.get(position), Toast.LENGTH_SHORT).show());
    return view;
}

我需要这个解决方案,因为我尝试使用 bundle 并没有任何反应。 感谢您的帮助

【问题讨论】:

  • 创建fragment时要传递吗?还是其他时间?
  • 你试过cartFragment.setArguments(bundle);但没有用?
  • @AnantaRaha 我想在创建片段时传递,我这样发起,当点击位置时,字符串正在发送并传递给片段并 .add 到 ArrayList
  • @Skizo-ozᴉʞS 它没有用,我更努力地使用 bundle
  • 检查我的答案。

标签: android string arraylist integer fragment


【解决方案1】:

您可能希望在创建时将 arraylist 传递到片段中,以便您可以在片段中使用它们。你应该使用Bundle。以下将正常工作:

CartFragment.java

ArrayList<String> cart_name = new ArrayList<>();
ArrayList<Integer> cart_price = new ArrayList<>();

/* Put a factory method for convenience */
public static CartFragment newInstance(ArrayList<String> names, ArrayList<Integer> prices) {
    Bundle args = new Bundle();
    args.putStringArrayList("names", names);
    args.putIntegerArrayList("prices", prices);
    CartFragment fragment = new CartFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    /* Retrieve your data, you can also retrieve inside onCreate() method */
    cart_name = getArguments("names").getStringArrayList();
    cart_price = getArguments("prices").getIntegerArrayList();

    ListView listView = view.findViewById(R.id.cart_list);
    Adapter adapter = new Adapter(getActivity(), cart_name, cart_price);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener((parent, v, position, id) ->
            Toast.makeText(getActivity(), cart_name.get(position), Toast.LENGTH_SHORT).show());
    return view;
}

MainActivity.java

// Do not write: CartFragment fragment = new CartFragment();
/* Create your fragment with your arguments */
CartFragment fragment = CartFragment.newInstance(sName, sPrice);

...

在传递参数之前,还要确保您的 sNamesPrice 不是空列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多