我在ListView项目中有一个按钮和Spinner。我已将下面的代码放在Spinner的 setOnItemSelectedListener 中。
String val = viewHolderItem.spinnerSize.getSelectedItem().toString();
shoeList.get(position).setSelectedSize(Integer.parseInt(val));
但它并没有在setOnItemSelectedListener内部发生变化。但是当我在Button的 setOnClickListener 中放入相同的代码时,它就会破坏并更改对象的值。这是什么原因?
GetView方法:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolderItem viewHolderItem;
final Shoe shoe = shoeList.get(position);
if(convertView == null){
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.component_order_shoe_row,null);
viewHolderItem = new ViewHolderItem();
viewHolderItem.tvName = (TextView)convertView.findViewById(R.id.tv_component_order_shoe_name);
viewHolderItem.tvPrice = (TextView)convertView.findViewById(R.id.tv_component_order_shoe_price);
viewHolderItem.tvDesc = (TextView)convertView.findViewById(R.id.tv_component_order_shoe_description);
viewHolderItem.ivPic = (ImageView)convertView.findViewById(R.id.iv_component_order_shoe_pic);
viewHolderItem.ivTick = (ImageView)convertView.findViewById(R.id.iv_component_order_shoe_tick);
viewHolderItem.spinnerSize = (Spinner)convertView.findViewById(R.id.spinner_component_order_shoe_size);
viewHolderItem.etQty = (EditText)convertView.findViewById(R.id.et_component_order_shoe_qty);
viewHolderItem.btnOrder = (Button)convertView.findViewById(R.id.btn_component_order_shoe_order);
viewHolderItem.btnOrder.setTag(viewHolderItem);
convertView.setTag(viewHolderItem);
}else{
viewHolderItem = (ViewHolderItem)convertView.getTag();
}
viewHolderItem.tvName.setText(shoe.getShoeName());
String text = String.valueOf(shoe.getPrice());
int integerPlaces = text.indexOf('.');
int decimalPlaces = text.length() - integerPlaces - 1;
if(decimalPlaces==1){
viewHolderItem.tvPrice.setText("Rs."+ text + "0");
}else{
viewHolderItem.tvPrice.setText("Rs."+ text);
}
if(shoe.isSelected()){
viewHolderItem.ivTick.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tick));
viewHolderItem.btnOrder.setText(mContext.getResources().getString(R.string.btn_remove_shoe_item));
}else{
viewHolderItem.ivTick.setImageDrawable(null);
viewHolderItem.btnOrder.setText(mContext.getResources().getString(R.string.btn_order_shoes));
}
viewHolderItem.tvDesc.setText(shoe.getDesc());
Bitmap bmp = AppControl.convertStringToBitmap(shoe.getImg());
viewHolderItem.ivPic.setImageBitmap(bmp);
int smallest = shoe.getSmallestSize();
int largest = shoe.getLargestSize();
Integer[] arr = getSizeArray(smallest,largest);
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(mContext, android.R.layout.simple_spinner_dropdown_item,arr);
viewHolderItem.spinnerSize.setAdapter(adapter);
viewHolderItem.btnOrder.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewHolderItem viewHolder = ((ViewHolderItem) v.getTag());
boolean isSelected = shoe.isSelected();
if(isSelected){
((Button)v).setText(mContext.getResources().getString(R.string.btn_order_shoes));
viewHolder.ivTick.setImageDrawable(null);
viewHolder.etQty.setEnabled(true);
isSelected = false;
}else{
((Button)v).setText(mContext.getResources().getString(R.string.btn_remove_shoe_item));
viewHolder.ivTick.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tick));
viewHolder.etQty.setEnabled(false);
isSelected = true;
}
shoeList.get(position).setSelected(isSelected);
String input = viewHolderItem.etQty.getText().toString();
if(isEnteredValueNumeric(input)){
shoeList.get(position).setQty(Integer.parseInt(input));
}else{
shoeList.get(position).setQty(1);
AppControl.showToastMessage(mContext,Constant.MSG_NOT_A_NUMBER);
viewHolderItem.etQty.setText(String.valueOf(1));
}
String val = viewHolderItem.spinnerSize.getSelectedItem().toString();
shoeList.get(position).setSelectedSize(Integer.parseInt(val));
}
});
viewHolderItem.spinnerSize.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String val = parent.getItemAtPosition(position).toString();
shoeList.get(position).setSelectedSize(Integer.parseInt(val));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
return convertView;
}
答案 0 :(得分:3)
你在这里弄错了:
String val = parent.getItemAtPosition(position).toString();
shoeList.get(position).setSelectedSize(Integer.parseInt(val));
shoeList的position
值与position
项的shoeSize Spinner
值不同。尝试将onItemSelected
方法position
名称参数更改为pos
之类的内容。因此,让getView
继续使用final int position
但onItemSelected
shoeSize
使用public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String val = parent.getItemAtPosition(pos)
.toString();
shoeList.get(position).setSelectedSize(
Integer.parseInt(val));
}
:
{{1}}
答案 1 :(得分:0)
在onClickListener中,它们不是onClickMethod中作为整数的位置的任何参数:
@Override
public void onClick(View v) {
/* so this position in shoeList.get(position) returns the position of the
* list view item i.e you array list exact position*/
shoeList.get(position).setSelectedSize(Integer.parseInt(val));
}
但是你在onItemSelected他们的位置值已经存在,所以你可能需要更改它的参数名称,如:
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
// just like @electrocrat insist
String val = parent.getItemAtPosition(pos)
.toString();
shoeList.get(position).setSelectedSize(
Integer.parseInt(val));
}
你也可以通过将标签设置为viewHolderItem.spinnerSize来实现 viewHolderItem.spinnerSize.setTag(position)和onItemSelected里面你可以通过(Integer)parent.getTag()得到位置,如:
shoeList.get((Integer)parent.getTag()).setSelectedSize(
Integer.parseInt(val));