如何将数据从Listview A传递给ListView B?

时间:2017-01-26 17:14:51

标签: android listview

我的购物车活动包含用户想要购买的商品。

enter image description here

我想要的是如果我点击“CHECK OUT”按钮,购物车中的所有数据都将被发送到下一个有ListView B的活动。

这是我的CartCustomerAdpater.class的一部分,我在其中设置textview字段

final Order data = items.get(position);
    holder.cart_name.setText(data.getName());
    holder.cart_price.setText("Php "+data.getPrice());

    holder.cart_qty.setText(data.getQty());

这是我的Cart.class的一部分,我的CHECK OUT按钮位于其中。

checkout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //WHAT SHOULD I DO HERE?
        }
    });

3 个答案:

答案 0 :(得分:0)

假设您声明了ListView:

ListView lv =(ListView) findViewById(R.id.lv);

对于listView使用setOnItemClickListener()的情况,这是我将数据发送到下一个Activity的示例代码。

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ModelClass jhild = (ModelClass) adapter.getItem(position);


                Intent in = new Intent(getApplicationContext(), BuyApartmentsInformation.class);
                in.putExtra(KEY_DISTRICT_NAME, jhild.getDistrict());
                in.putExtra(KEY_FLOORS, jhild.getFloors());
                in.putExtra(KEY_AREA, jhild.getArea());
                in.putExtra(KEY_BUYLAND_CODE, jhild.getBuyno());
                in.putExtra(KEY_PRICES, jhild.getPrice());
                in.putExtra(KEY_INFORMATION, jhild.getInformation());

                startActivity(in);
            }
        });

但我的建议你必须更新到RecyclerView

希望它运作良好。

答案 1 :(得分:0)

使用意图

发送

Intent intent = new Intent(A.this, B.class);
intent.putextra("keyName","value");
startActivity(intent);

收到

String data = getIntent().getExtras().getString("keyName");

答案 2 :(得分:0)

1. You need to implement Serializable or Parcelable in your **Order.java** class so that you can pass the ArrayList of Order through Bundle or Intent .

       public class Order implements Serializable{
        }

2. Pass the list of items through intent and start new activity

    checkout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent newActivity = new Intent(this , NewActivity.class);
                newActivity.putExtra("orderList" , items);
                startActivity(newActivity);
            }
        });

3. Get the list in NewActivity class

List<Order> itemsList = (List<Order>)getIntent.getSerializableExtra("orderList");

Hope it helps!