gpt4 book ai didi

点击位置的Android ListView

转载 作者:行者123 更新时间:2023-11-30 03:25:37 29 4
gpt4 key购买 nike

我有一个 View ,其中的行是通过 ListView 添加的。列表项在水平方向上有以下内容:复选框-复选框,项目名称 - textView,减号按钮 - 图像按钮,数量 - TextView ,添加按钮。 - 图片按钮

我需要根据增加或减少点击来更新数量。通过getTag()可以获取添加按钮的位置,但是无法获取相应的数量来更新。下面是适配器类

public class CustomAdapter extends BaseAdapter {

private List<String> mListItems;
private LayoutInflater mLayoutInflater;
private Context mContext;
ItemDBAdapter itemDB;
ViewHolder holder;
int a =1;
public CustomAdapter(Context context,ArrayList<String> arrayList){

mContext=context;
mListItems=arrayList;
mContext=context;
//get the layout inflater
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mListItems.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}


private static class ViewHolder {

TextView itemName,quantity;
CheckBox checkbox;
ImageButton addBtn,minusBtn;



}
@Override

public View getView(int position, View convertview, ViewGroup viewGroup) {

// create a ViewHolder reference


//check to see if the reused view is null or not, if is not null then reuse it
if (convertview == null) {
holder = new ViewHolder();


convertview = mLayoutInflater.inflate(R.layout.list_item, null);



/* holder.itemName=new TextView(mContext);
holder.checkbox=new CheckBox(mContext);
holder.quantity=new TextView(mContext);
holder.addBtn=new ImageButton(mContext);
holder.minusBtn=new ImageButton(mContext)*/;

holder.itemName = (TextView) convertview.findViewById(R.id.item_name);
holder.addBtn = (ImageButton) convertview.findViewById(R.id.add);
holder.minusBtn = (ImageButton) convertview.findViewById(R.id.minus);
holder.quantity = (TextView) convertview.findViewById(R.id.item_quantity);
holder.checkbox = (CheckBox) convertview.findViewById(R.id.cbBox);
holder.checkbox.setOnCheckedChangeListener(checkListener);
holder.checkbox.setTag(position);
holder.minusBtn.setImageResource(R.drawable.minus);
holder.quantity.setText(String.valueOf(a));
holder.quantity.setTag(position);
holder.addBtn.setImageResource(R.drawable.add);
holder.addBtn.setOnClickListener(addBtnClick);
holder.addBtn.setTag(position);
//holder.minusBtn.setOnClickListener(minusBtnClick);
holder.minusBtn.setTag(position);

// the setTag is used to store the data within this view
convertview.setTag(holder);
} else {
// the getTag returns the viewHolder object set as a tag to the view
holder = (ViewHolder)convertview.getTag();
}

//get the string item from the position "position" from array list to put it on the TextView
String stringItem = mListItems.get(position);

if (stringItem != null) {
if (holder.itemName != null) {
//set the item name on the TextView
holder.itemName.setText(stringItem);
}
}

//this method must return the view corresponding to the data at the specified position.
return convertview;

}


private OnClickListener addBtnClick = new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stube
int b = (Integer) v.getTag();
a = a + 1;
holder.quantity.setText(String.valueOf(a));

}
};



}

由于数量不可点击,我无法获得更新数量的特定位置。请指教。谢谢。

最佳答案

试试下面的

onClick

private OnClickListener addBtnClick = new OnClickListener() {

@Override
public void onClick(View v) {
View view = (View) v.getParent();
TextView tv = (TextView) view.findViewById(R.id.item_quantity);
int value = Integer.parseInt(tv.getText().toString());
value=value+1;
tv.setText(String.valueOf(value));
}
};

编辑:为了平滑滚动和性能,最好使用 ViewHolder

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

关于点击位置的Android ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18298607/

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