Paritosh
Paritosh

Reputation: 2137

Handle click listeners in a listView

I am working on custom adapter. I created separate class for it which extends BaseAdapter. I am having two images - (minus) and + (plus) which will decrease or increase quantity of product in list view.

List item looks like

[ - Product qty + ]

Now I already implemented listener for - (minus) image and it is working. But listener for + (plus) image is not working. I printed qty on the console it is incrementing but not getting updated in listview.

Here is the code

@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();
}

One more thing, if I delete item in list, it is getting deleted. But how can I get notification for deleted item in my main class. Consider I selected 3 items, now I removed any one item by clicking - (minus). The item is getting deleted from list - the code is in adapter class

notifyDataSetChanged(); 

But how can I update total amount which is getting calculated in main class

Upvotes: 0

Views: 101

Answers (2)

mmlooloo
mmlooloo

Reputation: 18977

try this:

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();
}

indeed you have not updated the data model so notifyDataSetChanged(); dose not take effect.

In order to send back updated data:

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()));

    }
});

and pass txtAmount to the constructor of your adapter and store it as txtAmountAdapter. now every update in total amount update txtAmount in main.

Upvotes: 1

Akash Moradiya
Akash Moradiya

Reputation: 3322

try this it may help you,

@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);
    }

}

Upvotes: 2

Related Questions