如何从列表视图项更改文本视图中的文本。我有一个 productAdapter,现在我尝试为这些项目添加一个 onclicklistener。当我单击它们时,我的列表视图项目中有 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;
}
}
希望有人可以帮助我:)