通过按钮

时间:2016-06-06 09:08:45

标签: android listview

我正在Android Studio中开发。 我有一个ListView,其中的每个项目都包含多个元素,包括2个按钮:增加/减少和EditText字段:edit_txt。 我需要在每次单击按钮时“增加”将增加edit_txt字段中的数字。 除了按下“减少”按钮之外,它还会减少edit_txt字段中的数字。 我该如何让它发挥作用?

该类名为“ProductList”,它扩展了AppCompatActivity。

package com.example.yuliaaa.myapplication;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;


public class ProductList extends AppCompatActivity {

    private List<myProductsView> myProducts_types = new ArrayList<myProductsView>();
    ArrayAdapter<myProductsView> adapter;

    private ArrayList<myProductsView>  choosen_items = new ArrayList<myProductsView>();
    ListView list;


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

        populateProductsList();
        populateListView();

    }

    private void populateProductsList() {
        myProducts_types.add(new myProductsView("p1", "d1", 1111, 12.90, R.drawable.cereal, 1));
        myProducts_types.add(new myProductsView("p2", "dddd", 1112, 10.90, R.drawable.cereal, 2));
        myProducts_types.add(new myProductsView("p3", "ffff", 1112, 30.00, R.drawable.cereal, 1));
        myProducts_types.add(new myProductsView("p4", "kkkkk", 1112, 20.00, R.drawable.cereal, 3));

    }

    private void populateListView() {
        adapter = new MyListAdapter();
        list = (ListView) findViewById(R.id.product_list);
        list.setAdapter(adapter);



    }
    public void StartCalck(View view){
        Intent intent = new Intent(ProductList.this, SplitBuying.class);
        startActivity(intent);
    }

    public void deleteItems(View view){


        for(int i=0;i<choosen_items.size();i++){
            adapter.remove(choosen_items.get(i));
        }

    }






    public class MyListAdapter extends ArrayAdapter<myProductsView>{



        public MyListAdapter(){
            super(ProductList.this, R.layout.pop_productlist, myProducts_types);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            //make sure we have a view to work with(may have been given null

            View itemView = convertView;

            if(itemView == null){
                itemView = getLayoutInflater().inflate(R.layout.product_item_view, parent, false);
            }
            //we need to populate the list
            //find the product to work with
            final myProductsView currentProduct = myProducts_types.get(position);



       //fill the view
            final CheckBox checkBox = (CheckBox)itemView.findViewById(R.id.product_checkBox);
            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    choosen_items.add(currentProduct);
                }
            });



            TextView productname = (TextView) itemView.findViewById(R.id.product_name);
            productname.setText(currentProduct.getProductName());

            EditText quantity = (EditText)itemView.findViewById(R.id.edit_text);
            quantity.setText(String.valueOf(currentProduct.getQuantity()));

            Button increase = (Button)itemView.findViewById(R.id.btn_plus);


            increase.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            return itemView;
        }

    }


    

}

班级 myProductsView 是代表产品的类。 productlist.xml 包含带有id:mylist的ListView。 而product_item_view.xml就是列表视图中每个项目的外观,包含: -Checkbox: “product_checkBox” -TextView: “PRODUCT_NAME” - 按钮: “btn_plus” -EditText: “edit_text” - 按钮: “btn_minus”

谢谢,

1 个答案:

答案 0 :(得分:0)

increase.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {  
                  myProducts_types.add(new myProductsView("p5", "abc", 100, 100.00, R.drawable.wheat, 4));
                  adapter.notifyDataSetChanged();
                }
            });