如何获得单击arrayadapter中的指定按钮?

时间:2018-12-24 05:45:01

标签: android-studio listview onclicklistener custom-arrayadapter

我是android的初学者,就我而言,我现在有一个阵列适配器,该适配器显示包含textview“-”,textview“ 0”和textview“ +”的产品的复选框列表。我的问题是当我想单击“ +”或“-”,但文本视图“ 0”的更改值在另一个复选框列表中。

有人可以帮助我解决我的问题吗?我真的需要帮助,因为我已经尝试了2天,但仍然一无所获。我仍然无法从单击的列表数组中获取ID或位置。

我尝试过这种解决方案,并通过调用方法changeCheckBox设置值:

        viewHolder.increase.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                countcheckBox++;
                changeCheckBox();
                produk.hitung();
            }
        });

        viewHolder.decrease.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                countcheckBox++;
                changeCheckBox();
                produk.hitung();
            }
        });

        public void changeCheckBox(){
            viewHolder.count.setText(countcheckBox+"");
        }

这是我的完整代码:

public class CheckboxAdapter extends BaseAdapter {
private Context activity;
private ArrayList<ProductModel> productItems;
private static LayoutInflater inflater = null;
private View vi;
private ViewHolder viewHolder;
ProdukLaundry produk;
public int countcheckBox = 0;

 public CheckboxAdapter(Context context, ArrayList<ProductModel> items, ProdukLaundry produk) {
    this.activity      = context;
    this.productItems  = items;
    this.produk        = produk;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Object getItem(int i) {
    return i;
}

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

@Override
public View getView(final int position, View view, ViewGroup parent) {
    vi = view;
    final int pos = position;
    ProductModel items = productItems.get(pos);

    if(view == null) {
        vi                  = inflater.inflate(R.layout.list_produk, null);
        viewHolder          = new ViewHolder();
        viewHolder.checkBox = (CheckBox) vi.findViewById(R.id.checkBox_productName);
        viewHolder.decrease = (TextView) vi.findViewById(R.id.decrease_product);
        viewHolder.count    = (TextView) vi.findViewById(R.id.count_product);
        viewHolder.increase = (TextView) vi.findViewById(R.id.increase_product);
        viewHolder.price    = (TextView) vi.findViewById(R.id.product_price);
        vi.setTag(viewHolder);
        viewHolder.checkBox.setText(items.getProduct_name());
        viewHolder.price.setText(items.getProduct_price());

        viewHolder.increase.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                countcheckBox++;
                changeCheckBox();
                produk.hitung();
            }
        });

        viewHolder.decrease.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                countcheckBox++;
                changeCheckBox();
                produk.hitung();
            }
        });
    }else{
        viewHolder = (ViewHolder) view.getTag();
        viewHolder.checkBox.setText(items.getProduct_name());
        viewHolder.price.setText(items.getProduct_price());
    }
    if(items.isCheckbox()){
        viewHolder.checkBox.setChecked(true);
    } else {
        viewHolder.checkBox.setChecked(false);
    }

    return vi;
}

public void changeCheckBox(){
        viewHolder.count.setText(countcheckBox+"");
}

public ArrayList<ProductModel> getAllData(){
    return productItems;
}

public void setCheckBox(int position){
    ProductModel items = productItems.get(position);
    items.setCheckbox(!items.isCheckbox());
    notifyDataSetChanged();
}

public class ViewHolder{
    TextView decrease, count, increase, price;
    CheckBox checkBox;
}

}

我期望的是,当我单击“ +”或“-”按钮时,更改的textview的值在该按钮单击的列表中。

0 个答案:

没有答案