我有一个listview,它使用自定义listAdapter进行设置。 每个listView项中都有一个按钮来更改同一行中的textView。我该怎么做?以下是代码的一部分:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final int _position = position;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.activity_today_sales_list_view, null);
holder = new ViewHolder();
holder.txt_itemName = (TextView) convertView.findViewById(R.id.itemNameTB);
holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.priceTB);
holder.txt_plusBtn = (Button) convertView.findViewById(R.id.addBtn);
holder.txt_minusBtn = (Button) convertView.findViewById(R.id.minusBtn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_plusBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Plus Btn", "Clicked");
}
});
holder.txt_minusBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("Minus Btn", "Clicked");
}
});
holder.txt_itemName.setText(itemDetailsrrayList.get(position).getItemName());
holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getItemCost());
return convertView;
}
答案 0 :(得分:10)
您可以使用以下代码来解决问题。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.activity_today_sales_list_view, null);
holder = new ViewHolder();
holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.priceTB);
holder.txt_plusBtn = (Button) convertView.findViewById(R.id.addBtn);
holder.txt_plusBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewHolder holder1 = (ViewHolder)v.getTag();
//Access the Textview from holder1 like below
holder1.txt_itemPrice.setText("Plus");
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_plusBtn.setTag(holder);
holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getItemCost());
return convertView;
}
答案 1 :(得分:0)
试试这个
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final int _position = position;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.activity_today_sales_list_view, null);
holder = new ViewHolder();
holder.txt_itemName = (TextView) convertView.findViewById(R.id.itemNameTB);
holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.priceTB);
holder.txt_plusBtn = (Button) convertView.findViewById(R.id.addBtn);
holder.txt_minusBtn = (Button) convertView.findViewById(R.id.minusBtn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_plusBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Plus Btn", "Clicked");
holder.txt_itemName.setText("Your String");
holder.txt_itemPrice.setText("Your String");
}
});
holder.txt_minusBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("Minus Btn", "Clicked");
holder.txt_itemName.setText("Your String");
holder.txt_itemPrice.setText("Your String");
}
});
holder.txt_itemName.setText(itemDetailsrrayList.get(position).getItemName());
holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getItemCost());
return convertView;
}
希望这有帮助。
答案 2 :(得分:0)
设置Button和TextView的ID,您可以在单击按钮时区分它们。我的意思是..假设您有10个要在列表视图中显示的项目,因此getView()
方法将从0到9接收position
。那么您可以在此处设置按钮的ID例如10+position
和TextViews的1000+position
或任意随机数。
因此,当单击一个按钮时,您可以获取它的id,从中减去10.现在添加1000以获取TextView并更改它的文本。
答案 3 :(得分:0)
以下是我自己的示例,说明如何在onClickListener
内正确使用Adapter
。功能与您几乎相同。我有一个产品列表,在我的列表项中,用户可以增加或减少购买的商品数量。
我的项目对象包含项目属性。它有一个ID,一个名称和一个订单,指的是该商品的购买次数:
public class MenuItem {
private int id;
private String name;
private int order = 0;
public MenuItem(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
}
以下是我的Adapter
的片段,其中包含MenuItems列表。 UI包含一个向上和向下图像按钮以及名称。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater lInflater = ((Activity) mContext).getLayoutInflater();
convertView = lInflater.inflate(R.layout.menu_item, parent, false);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.menu_name);
holder.details = (TextView) convertView.findViewById(R.id.menu_details);
holder.counter = (TextView) convertView.findViewById(R.id.menu_counter);
holder.up = (ImageView) convertView.findViewById(R.id.menu_up);
holder.down = (ImageView) convertView.findViewById(R.id.menu_down);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(getMenuList().get(position).getName());
holder.details.setText(getMenuList().get(position).getDetails());
holder.counter.setText(String.valueOf(getMenuList().get(position).getOrder()));
holder.up.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onListItemClick(position, UP);
}
});
holder.down.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onListItemClick(position, DOWN);
}
});
return convertView;
}
protected void onListItemClick(int position, int direction) {
int numberOfOrders = getMenuList().get(position).getOrder();
if (direction == DOWN && numberOfOrders == 0) {
// Don't go negative!
return;
}
getMenuList().get(position).setOrder(numberOfOrders + direction);
// in your case: itemDetailsrrayList.get(position).setOrder(...);
notifyDataSetChanged();
}
玩得开心!