如何调用适配器中的每个对象?

时间:2018-12-23 03:49:00

标签: android onclick android-arrayadapter checkboxlist

我有一个阵列适配器的列表,这些适配器具有以下布局,其中包含复选框,textview增加,textview计数和textview减少: layout product adapter。 问题是如何在适配器上显示的每个对象上从ProdukActivity.java调用或实现clicklistener。我只想知道它如何与适配器中的对象一起使用,以便我可以在选中每个复选框时计算产品价格。

这是我的适配器代码:

@supports (-ms-ime-align: auto) {
  /* Edge specific styles */
}

这是我的ProdukActivity.java:

public class ProductAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<ProductModel> productItems;

    public ProductAdapter(Activity activity, List<ProductModel> productItems) {
        this.activity = activity;
        this.productItems = productItems;
    }

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

    @Override
    public Object getItem(int location) {
        return productItems.get(location);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_produk, null);

        CheckBox namaProduk   = (CheckBox) convertView.findViewById(R.id.checkBox_productName);
        TextView kurangProduk = (TextView) convertView.findViewById(R.id.decrease_product);
        TextView hitungProduk = (TextView) convertView.findViewById(R.id.count_product);
        TextView tambahProduk = (TextView) convertView.findViewById(R.id.increase_product);
        TextView hargaProduk  = (TextView) convertView.findViewById(R.id.product_price);

        ProductModel produk = productItems.get(position);
        namaProduk.setText(produk.getProduct_name());
        hargaProduk.setText(produk.getProduct_price());

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

在适配器getView内,为以下每个对象添加侦听器。然后在听众内部进行处理

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_produk, null);

    final CheckBox namaProduk   = (CheckBox) convertView.findViewById(R.id.checkBox_productName);
    TextView kurangProduk = (TextView) convertView.findViewById(R.id.decrease_product);
    TextView hitungProduk = (TextView) convertView.findViewById(R.id.count_product);
    TextView tambahProduk = (TextView) convertView.findViewById(R.id.increase_product);
    TextView hargaProduk  = (TextView) convertView.findViewById(R.id.product_price);

    ProductModel produk = productItems.get(position);
    namaProduk.setText(produk.getProduct_name());
    hargaProduk.setText(produk.getProduct_price());

    namaProduk.setOnCheckedChangeListener(
            new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(isChecked){
                        //check here
                    }
                }
            }
    );

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

                    //do your task here
                }
            }
    );

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

                    //do your task here
                }
            }
    );

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

                    //do your task here
                }
            }
    );

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

                    //do your task here
                }
            }
    );

    return convertView;
}