I'm working on an android app. I have found how to put dynamically a Spinner in each row. I need that every spinner must be used before the list will be sent to a DB with a click on a button. My problem is with spinners how to check if every spinner was used? Is there a library or something else?
My code: The spinner's name is "viewHolder.spin
class ViewHolder{
protected TextView text;
protected Spinner spin;
public TextView coeffTV;
}
public View getView(int position, View convertView, ViewGroup parent){
View view = null;
LayoutInflater inflator = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
view = inflator.inflate(R.layout.rowview, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView)view.findViewById(R.id.label);
viewHolder.spin = (Spinner)view.findViewById(R.id.spin_sous_rubrique);
viewHolder.coeffTV = (TextView)view.findViewById(R.id.coeffTV);
final String[] items = {" ","juste","faux"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
viewHolder.spin.setAdapter(adapter);
Each spinner should implement an OnItemSelectedListener that provides 2 methods, onItemSelected and onNothingSelected. You can make a boolean for each spinner or have a list to contain all the booleans. Then in the onItemSelected method, you can set the boolean to true if it an item was selected.
//List of booleans
boolean[] spinnersClicked= new boolean[numberOfSpinners-1];
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//Item was clicked, set boolean to true
spinnersClicked[i]=true;
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
Now when someone clicks on your submit to database button, you can loop through your array of booleans and see if they are all used.
public boolean allSpinnersTrue(boolean[]bools)
{
for(boolean spinnerClicked:bools)
{
if(!spinnerClicked)
//At least one spinner isn't in use
return false;
}
//All spinners are used
return true;
}