在列表视图中添加/删除产品到购物车

时间:2016-06-06 12:01:31

标签: android listview

我正在尝试制作带有订购功能的餐厅菜单。有一个简单的列表视图,其中包含产品名称,价格和添加,删除按钮。我想要的是当用户点击“添加”按钮,产品名称和价格被添加到arraylist或其他东西,以便我可以在另一个活动中获取此列表并显示总价格。我可以使用SQLite存储项目,但后来我不知道如何获得每个选定产品的价格(在第二个活动中)进行添加。是否有任何特定的方法来存储项目,如使用POJO和适配器?

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private ArrayList<Model> mListData;
    private CustomListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView)findViewById(R.id.list);

        mListData = new ArrayList<>();

        mListData.add(new Model("Beef Manhattan", "50"));
        mListData.add(new Model("Chicken Fried Steak", "90"));
        mListData.add(new Model("Corned Beef", "100"));
        mListData.add(new Model("Domesticated Turkey", "80"));
        mListData.add(new Model("Eggs Benedict", "10"));
        mListData.add(new Model("French Dip", "20"));
        mListData.add(new Model("Green Bean Casserole", "30"));
        mListData.add(new Model("Potato Salad", "40"));
        mListData.add(new Model("Pumpkin Pie", "60"));
        mListData.add(new Model("Salisbury Steak", "70"));

        adapter = new CustomListAdapter(this, R.layout.listrow, mListData);
        listView.setAdapter(adapter);

    }

}
public class CustomListAdapter extends ArrayAdapter<Model> {

    private Context mContext;
    int resource;
    private ArrayList<Model> mListData = new ArrayList<Model>();


    public CustomListAdapter(Context mContext, int resource, ArrayList<Model> mListData) {
        super(mContext, resource, mListData);
        this.resource = resource;
        this.mContext = mContext;
        this.mListData = mListData;
    }

    public void setListData(ArrayList<Model> mListData) {
        this.mListData = mListData;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return super.getCount();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        if (v == null) {
            holder = new ViewHolder();

            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();

                v = inflater.inflate(resource, parent, false);

                holder.name = (TextView) v.findViewById(R.id.name);
                holder.rate = (TextView) v.findViewById(R.id.rate);
                holder.add = (Button) v.findViewById(R.id.add);
                holder.remove = (Button) v.findViewById(R.id.remove);


            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }




        Model item = mListData.get(position);

            holder.name.setText(item.getName());
            holder.rate.setText(item.getRate());

        return v;
    }

    class ViewHolder {

        TextView name;
        TextView rate;
        Button add;
        Button remove;
    }

}
public class Model {

    private String name, rate;

    public Model(String name, String rate) {
        this.name = name;
        this.rate = rate;
    }

    public String getName() {
        return name;
    }

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

    public String getRate() {
        return rate;
    }

    public void setRate(String rate) {
        this.rate = rate;
    }


}

1 个答案:

答案 0 :(得分:0)

这可以帮助您

在模型类中创建一个布尔变量isSelected,并将初始值指定为false,当他选择该行或单击该对象的add make isSelected变量为true时。并将该对象的ArrayList传递给第二个活动,并使用foreach或for循环和ckeck for isSelected,如果isSelected为true则获取ammount并添加。而已。试一试它会起作用:)