This is what my Activity looks like:
I have an arrayList which is already defined with a few Item objects.
ArrayList<Item> selectedItems = new ArrayList<Item>();
The following code dynamically creates multiple views and adds them to a linearlayout based on how many there are within the ArrayList selectedItems. In this case we only have 2 Item objects which present us with the value Single Potrait and Makeup as we can see from the screenshot. I attempted to assign an onClickListener however I am unable to determine which view is being clicked.
How can I assign a onClickListener to each view where I am able to determine which one is clicked so I can change the Quantity TextView for that specific item. I would like to avoid using a ListView in this situation for other reasons.
In other words: If someone clicks "Makeup" I want to be able to have a reference to that created view so that I can edit the values such as the Price or Quantity however the difficulty is that I do not know how to reference the view or even how to determine which view was selected. Thank you for all the help!
for(Item currentItem : selectedItems)
{
LayoutInflater inflater = (LayoutInflater)this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View item = inflater.inflate(R.layout.product_template, productField,false);
item.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//Add Code to open Dialog for Changing Quantity
//HOW DO I GET ID OF THE VIEW THAT IS CLICKED
//So I can change the text within it?
}
});
//Set the Name
TextView name = (TextView) item.findViewById(R.id.TextView1);
name.setText(currentItem.getTitle());
//Set the Quantity
TextView quantity = (TextView)item.findViewById(R.id.TextView2);
quantity.setText(""+ currencyFormat.format(currentItem.getQuantity()));
productField.addView(item);
}
}
This is the Item.java code if needed for additional reference.
public class Item {
private String title;
private double price, quantity;
private int position;
public Item(String title, double price) {
super();
this.title = title;
this.price = price;
quantity = 1.0;
}
public Item(Item item, int position) {
super();
this.title = item.getTitle();
this.price = item.getPrice();
quantity = 1.0;
this.position = position;
}
public String getTitle() {
return title;
}
public String getDescription(){
return description;
}
public double getPrice() {
return price;
}
public void setQuantity(double quantity){
this.quantity = quantity;
}
public double getQuantity(){
return quantity;
}
public int getPosition(){
return position;
}
@Override
public String toString() {
return title+"\n$ "+price;
}
}