gpt4 book ai didi

java - 动态地将 onClickListener 分配给膨胀的 View

转载 作者:行者123 更新时间:2023-12-02 05:52:57 24 4
gpt4 key购买 nike

这就是我的 Activity :

enter image description here

我有一个 arrayList,它已经定义了一些 Item 对象。

ArrayList<Item> selectedItems = new ArrayList<Item>();

以下代码动态创建多个 View ,并根据 ArrayList selectedItems 中的数量将它们添加到线性布局中。在本例中,我们只有 2 个 Item 对象,它们向我们提供值 Single Potrait 和 Makeup,正如我们从屏幕截图中看到的那样。我尝试分配一个 onClickListener 但无法确定正在单击哪个 View 。

如何为每个 View 分配一个 onClickListener,以便我能够确定单击了哪个 View ,以便我可以更改该特定项目的 Quantity TextView。由于其他原因,我想避免在这种情况下使用 ListView。

换句话说:如果有人单击“化妆”,我希望能够引用该创建的 View ,以便我可以编辑价格或数量等值,但困难在于我不知道如何引用 View ,甚至如何确定选择了哪个 View 。感谢您的所有帮助!

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

如果需要额外引用,这是 Item.java 代码。

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

}

最佳答案

我看到两种解决方案,第一个是使用 setTag 方法( http://developer.android.com/reference/android/view/View.html#setTag(java.lang.Object) )将每个项目附加到每个 View ,然后在监听器上检索它以了解您正在处理哪个对象。

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

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

第二个解决方案是将项目传递给您的监听器

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

关于java - 动态地将 onClickListener 分配给膨胀的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23375760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com