如何从listview项目更改textview中的文本。 我有一个productAdapter,现在我尝试为这些项目添加onclicklistener。 当我点击它们时,我的listview项目中有2个按钮,文本应该更改。 onclicklistener工作顺便说一下,我点击了所点击的项目的位置。
public class ProductAdapter extends ArrayAdapter<Product>{
Context context;
int layoutResourceId;
List<Product> data = null;
public ProductAdapter(Context context, int layoutResourceId, List<Product> yal) {
super(context, layoutResourceId, yal);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = yal;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
prodHolder holder = null;
final int pos=position;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new prodHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.txtPrice = (TextView)row.findViewById(R.id.txtPrice);
holder.txtAmount = (TextView)row.findViewById(R.id.txtAmount);
holder.minus = (Button)row.findViewById(R.id.minbtn);
holder.plus = (Button)row.findViewById(R.id.plusbtn);
row.setTag(holder);
}
else
{
holder = (prodHolder)row.getTag();
}
holder.minus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onMinButtonClicked(pos,prod,v);
}
});
holder.plus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onPlusButtonClicked(pos,prod,v);
}
});
return row;
}
private void onMinButtonClicked(int position,Product prod,View row) {
short am= prod.getAmount();
if(am>0){
am=(short) (am-1);
}
prod.setAmount(am);
SerializeIt si=new SerializeIt();
si.storeMyData(prod,context);
}
private void onPlusButtonClicked(int position,Product prod,View row) {
short tt=(short) (prod.getAmount()+1);
prod.setAmount(tt);
SerializeIt si=new SerializeIt();
si.storeMyData(prod,context);
}
static class prodHolder
{
ImageView imgIcon;
TextView txtTitle;
TextView txtPrice;
TextView txtAmount;
Button minus;
Button plus;
}
}
希望有些人可以帮助我:)。
答案 0 :(得分:1)
尝试使用以下代码..
holder.minus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.txtAmount.settext("MINUS");
}
});
holder.plus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.txtAmount.settext("PLUSH");
}
});