这就是我的活动:
我有一个已经用几个Item对象定义的arrayList。
ArrayList<Item> selectedItems = new ArrayList<Item>();
以下代码动态创建多个视图,并根据ArrayList selectedItems中的数量将它们添加到linearlayout。在这种情况下,我们只有2个Item对象,我们可以从屏幕截图中看到Single Potrait和Makeup的值。我尝试分配onClickListener,但是我无法确定单击哪个视图。
如何为每个视图分配onClickListener,我可以确定单击哪一个,这样我就可以更改该特定项目的Quantity TextView。我想在其他原因避免在这种情况下使用ListView。
换句话说:如果有人点击“化妆”,我希望能够引用该创建的视图,以便我可以编辑价格或数量等值,但难度是我不知道如何引用视图甚至如何确定选择了哪个视图。谢谢你的帮助!
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;
}
}
答案 0 :(得分:2)
我看到两个解决方案,第一个是使用setTag方法(http://developer.android.com/reference/android/view/View.html#setTag(java.lang.Object))将每个项目附加到每个视图,然后在您的侦听器上检索它以了解您正在处理的对象。
.
.
.
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));
答案 1 :(得分:0)
而不是
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()));
使用
name.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
v.getId();
// Name TextView Clicked here.
}
});
答案 2 :(得分:0)
我可能会在ListView上建议一些更明智的东西吗?
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");
}
});
答案 3 :(得分:0)
继承Activity
以实现OnClickListener
并注册点击监听器,如下所示:
name.setOnClickListener(this);
然后覆盖onClick()
的{{1}}方法并处理点击事件:
OnClickListener