Question

This is what my Activity looks like:

enter image description here

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

}

Was it helpful?

Solution

I see two solutions, the first one is to attach each Item to the each view using the setTag method (http://developer.android.com/reference/android/view/View.html#setTag(java.lang.Object)), then on you listener retrieve it to know which objet you are dealing with.

.
.
.
item.setTag(currentItem );
item.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {

            Item it = (Item) v.getTag();
        }
    });
.
.
.

The second solution would be to pass the item to your listener

private class CustomClickListener implements View.OnClickListener{
     private Item mIt;
     CustomClickListener (Item it){
        this.mIt= it;
     }

     public void onClick(View v) 
        {

            // The item that was clicked it mIt
        }
}

// When you create the views
item.setOnClickListener(new CustomClickListener(currentItem));

OTHER TIPS

Instead of

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

        /*
         * add android:clickable-"true"
         * or set Programatically,
         */
        name.setClickable(true);

        //Set the Quantity
        TextView quantity = (TextView)item.findViewById(R.id.TextView2);
        quantity.setText(""+ currencyFormat.format(currentItem.getQuantity()));

use

name.setOnClickListener(new View.OnClickListener() 
   {
     public void onClick(View v) 
      {
           v.getId();
           // Name TextView Clicked here.
       }
   });

Might I suggest something a little more sane on the ListView instead?

    this.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<Item> adapterView, View view, int position, long id) {
              Item item = getItemAtPosition(position);  // your actual item
              // something interesting with that knowledge.
              TextView quantity = (TextView)view.findViewById(R.id.TextView2);
              quantity.setText("someValue");
        }
    });

Inherit the Activity to implement OnClickListener and register the click listeners like this:

name.setOnClickListener(this);

Then override the onClick() method of OnClickListener and handle the click event:

public void onClick(View view) 
{
       if (view.getId() == <TextViewId>)
       {
           //do something
       }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top