将 Activity 中的字符串和整数传递给片段

Pass String and Integer from Activity to Fragment

我的应用程序有问题,你能给我解决方案吗? 我需要将字符串和整数(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 尝试过,但没有任何反应。 感谢您的帮助

您可能希望在创建时将 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 不是空列表。