Reputation: 3713
I'm trying to find out why spinner is conflicting with checkBox inside a listView. If I comment the spinner lines, the checkBox listener works fine, I can select it, check if is selected and delete the row of the List. If I let the spinner declaration, as it is in the code below, the listener works, I can select the checkBox but when I try to get the selected Item (at checkToDelete method) the return of the selected checkBox is always empty (like there is nothing selected). I need the spinner to check the quantity of items and the checkBox to delete the row.
Anyway, I've been researching a solution for 2 days and wouldn't like to let this problem go before know what is going on and prevent future mistakes. One possible solution I found is to avoid the use of spinner and instead use a button with AlertDialog
Does anyone know what I'm missing here?
@Override
public View getView(final int position,View convertView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.menu_itens, viewGroup
, false);
String itemName = MENU_ITEM.get(position);
TextView item = (TextView) rowView.findViewById(R.id.item);
item.setText(itemName);
imageView = (ImageView) rowView.findViewById(R.id.itemImage);
checkBox[position] = (CheckBox) rowView.findViewById(R.id.check);
checkBox[position].setTag(itemName);
checkBox[position].setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
checkBox[position].setSelected(buttonView.isChecked());
}
});
spinner[position] = (Spinner) rowView.findViewById(R.id.quantity);
spinner[position].setAdapter(spinnerAdapterArray[position]);
spinner[position].setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
//Listener options
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
return rowView;
}
public List<Integer> checkListToDelete() {
List<Integer> aux = new ArrayList<Integer>();
for(int i=0; i<MENU_SIZE; i++){
System.out.println(checkBox[i].isChecked());
if(checkBox[i].isChecked()){
checkBox[i].setChecked(false);
aux.add(i);
}
}
return aux;
}
Upvotes: 0
Views: 588
Reputation: 6699
private List<Boolean> mCheckList;
@Override
public View getView(final int position,View convertView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.menu_itens, null);
holder.textview = (TextView) convertView.findViewById(R.id.item);
holder.imageView = (ImageView) convertView.findViewById(R.id.itemImage);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.check);
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mCheckList.set(position, isChecked);
}
});
holder.spinner = (Spinner) convertView.findViewById(R.id.quantity);
holder.spinner.setAdapter(spinnerAdapterArray[position]);
holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
//Listener options
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
String itemName = MENU_ITEM.get(position);
holder.textview.setText(itemName);
holder.checkbox.setChecked(mCheckList.get(position));
return convertView;
}
public List<Integer> checkListToDelete() {
List<Integer> aux = new ArrayList<Integer>();
for(int i=0; i<MENU_SIZE; i++){
System.out.println(mCheckList.get(i));
if(mCheckList.get(i)){
aux.add(i);
}
mCheckList.set(i, false);
}
notifyDataSetChanged();
return aux;
}
class ViewHolder {
TextView textview;
ImageView imageview;
Spinner spinner;
CheckBox checkbox;
}
Upvotes: 1