已将商品添加到购物车清单

时间:2018-12-03 09:46:04

标签: android

我创建了一个android应用程序。在该应用程序中,当我单击添加时,项目已添加到阵列列表中,并且该阵列列表要显示在购物车活动中。该怎么做。

这是我的活动1

public void addShirt(View view) {
    MainActivity.cartItems.add(getString(R.string.shirt));
}
public void addPant(View view) {
    MainActivity.cartItems.add(getString(R.string.pant));
}
public void view(View view) {
    Intent i =new Intent(OnlyIron.this,CartActivity.class);
    startActivity(i);
}

和购物车活动为

for(int i=0; i<MainActivity.cartItems.size();i++) {
        Toast.makeText(this, "item : " + MainActivity.cartItems.get(i), Toast.LENGTH_SHORT).show();

    }

它显示吐司,但我在此处输入代码,希望此显示在列表视图中

2 个答案:

答案 0 :(得分:0)

首先让Create模型类在其中存储数据。

public class Cart {
   private String id;
   private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

现在要在该列表中添加项目,我会这样称呼:

 private ArrayList<Cart > cart_array;
 cart_array= new ArrayList<>();

Cart cart1 = new Cart ();
     cart.setId("1");
     cart.setName("first product");

Cart cart2 = new Cart ();
     cart.setId("2");
     cart.setName("first product");

Cart cart3 = new Cart ();
     cart.setId("3");
     cart.setName("first product");

//and than add your model into array 


cart_array.add(cart1);
cart_array.add(cart2);
cart_array.add(cart3);

//and finaly set your adapter 

Cart_Adapter adapter = new Cart_Adapter(cart_array, getActivity());
Recycler.setAdapter(adapter );

答案 1 :(得分:0)

请在 CartActivity 中查看列表视图,并根据需要创建自定义适配器(用户界面)。将 MainActivity.cartItems 传递给适配器。它会开始显示在您的 CartActivity 中。

您可以看到以下示例:

公共类CustomAdapter扩展了BaseAdapter {

     Activity mContext;
       public ArrayList<String> mCartList = new ArrayList<String>();
        private LayoutInflater mInflater=null;

        public CustomAdapter(Activity activty, ArrayList<String> list)
        {
            this.mContext = activty;
            mInflater = activty.getLayoutInflater();
            this.mCartList=list;

        }

        @Override
        public int getCount() {
            if (mCartList != null){
                return mCartList.size();
            } else{
                return 0;
            }
        }

        @Override
        public String getItem(int arg0) {
            return mCartList.get(arg0);
        }

        @Override
        public long getItemId(int index) {
            return index;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup arg2) {
            final ViewHolder holder;
            if (convertView == null ) {        
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.item_layout, null);             
                holder.mItemNameTV= (TextView) convertView.findViewById(R.id.itemtv);        
                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }

             holder.mNameTV.setText(mCartList.get(position));        

            return convertView;
        }


        private static class ViewHolder {
            TextView mNameTV;

        }

    }

    // Item Layout 

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="@dimen/forty_dp"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <TextView
            android:id="@+id/fieldTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/five_dp"
            android:layout_weight="0.4"
            android:padding="@dimen/ten_dp"
            android:text="Custom Field"
            android:textColor="@color/dark_gray_color"
            android:textSize="@dimen/normal_font_size"
            android:visibility="visible" />        

    </LinearLayout>


// Let Suppose your CartActivity is following:

 ListView mListView = (ListView)findViewById(R.id.listview);
 CustomAdapter adapter = new CustomAdapter(this, MainActivity.cartItems);
 mListView.setAdapter(adapter);