处理listView中的单击侦听器

时间:2014-08-07 12:54:00

标签: android listview android-listview

我正在研究自定义适配器。我为它创建了单独的类,扩展了BaseAdapter。我有两个图像 - (减号)和+(加号)会减少或增加列表视图中的产品数量。

列表项看起来像

[ - 产品数量+]

现在我已经实现了 - (减号)图像的监听器,它正在工作。但+(加号)图像的监听器无法正常工作。我在控制台上打印了数量正在增加但没有在listview中更新。

这是代码

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list_row_sold_item, null);

    TextView txtListItem = (TextView) vi.findViewById(R.id.txtListItem);
    txtQuantity = (TextView) vi.findViewById(R.id.txtQuantity);
    ImageView imgCancel = (ImageView) vi.findViewById(R.id.imgCancel);
    ImageView imgPlus = (ImageView) vi.findViewById(R.id.imgPlus);

    HashMap<String, String> mapData = new HashMap<String, String>();
    mapData = data.get(position);

    txtListItem.setText(mapData.get("product"));
    txtQuantity.setText(mapData.get("qty"));

    imgCancel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            doButtonOneClickActions(position);
        }
    });

    imgPlus.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            qtyClickAction(position);
        }
    });

    return vi;
}

private void qtyClickAction(int rowNumber) {
    System.out.println(rowNumber);
    int qty = Integer.parseInt(txtQuantity.getText().toString().trim());
    System.out.println("before : " + qty);
    qty++;
    txtQuantity.setText("" + qty);
    System.out.println("after : " + qty);
    notifyDataSetChanged();
}

private void doButtonOneClickActions(int rowNumber) {
    // Do the actions for Button one in row rowNumber (starts at zero)
    System.out.println("rowNumber : " + rowNumber);
    int qty = Integer.parseInt(txtQuantity.getText().toString().trim());
    if (qty == 1) {
        data.remove(rowNumber);

    } else {
        txtQuantity.setText("" + --qty);
    }
    notifyDataSetChanged();
}

还有一件事,如果我删除列表中的项目,它将被删除。但是如何在主班中获取已删除项目的通知。考虑我选择了3个项目,现在我通过单击 - (减号)删除任何一个项目。该项目将从列表中删除 - 代码位于适配器类

notifyDataSetChanged(); 

但是如何更新在主课程中计算的总金额

2 个答案:

答案 0 :(得分:2)

试试这可能对你有帮助,

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list_row_sold_item, null);

    TextView txtListItem = (TextView) vi.findViewById(R.id.txtListItem);
    TextView txtQuantity = (TextView) vi.findViewById(R.id.txtQuantity);
    ImageView imgCancel = (ImageView) vi.findViewById(R.id.imgCancel);
    ImageView imgPlus = (ImageView) vi.findViewById(R.id.imgPlus);

    HashMap<String, String> mapData = new HashMap<String, String>();
    mapData = data.get(position);

    txtListItem.setText(mapData.get("product"));
    txtQuantity.setText(mapData.get("qty"));

    imgCancel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            doButtonOneClickActions(txtQuantity,position);
        }
    });

    imgPlus.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            qtyClickAction(txtQuantity,position);
        }
    });

    return vi;
}

private void qtyClickAction(TextView txtQuantity,int rowNumber) {
    System.out.println(rowNumber);
    int qty = Integer.parseInt(txtQuantity.getText().toString().trim());
    System.out.println("before : " + qty);
    qty++;
    txtQuantity.setText("" + qty);
    System.out.println("after : " + qty);
}

private void doButtonOneClickActions(TextView txtQuantity,int rowNumber) {
    // Do the actions for Button one in row rowNumber (starts at zero)
    System.out.println("rowNumber : " + rowNumber);
    int qty = Integer.parseInt(txtQuantity.getText().toString().trim());
    if (qty == 1) {
        data.remove(rowNumber);
        notifyDataSetChanged();
    } else {
        txtQuantity.setText("" + --qty);
    }

}

答案 1 :(得分:1)

试试这个:

private void qtyClickAction(TextView txtQuantity,int rowNumber) {
System.out.println(rowNumber);
int qty = Integer.parseInt(txtQuantity.getText().toString().trim());
System.out.println("before : " + qty);
qty++;
data.get(rowNumber).set("qty")=qty;
//txtQuantity.setText("" + qty); not needed anymore
System.out.println("after : " + qty);
notifyDataSetChanged();
}

确实你没有更新数据模型所以notifyDataSetChanged();剂量不起作用。

为了发回更新的数据:

imgCancel.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        doButtonOneClickActions(position);
        // update totalAmount
         txtAmountAdapter.setText(Integer.valueOf(totalAmount).toString()));

    }
});

imgPlus.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        qtyClickAction(position);
         // update totalQty
        txtAmountAdapter.setText(Integer.valueOf(totalAmount).toString()));

    }
});

并将txtAmount传递给适配器的构造函数并将其存储为txtAmountAdapter。现在每个更新的总量都在主要更新txtAmount。