Home > front end >  how I select element of spinner
how I select element of spinner

Time:09-26

//this my main result where we declare all editText and spinner

   public class Addresult extends 
   AppCompatActivity {
  String subject[] = {"English", "Urdu", "Math", 
 "Biology", "Chemistry", "Physics", "Computer"};
  Spinner sp1, sp2, sp3, sp4, sp5, sp6, sp7;
  EditText eng, urdu, math, bio, chem, phy, 
  comp, 
  r;
   Database database;

   @Override
   protected void onCreate(Bundle s 
   avedInstanceState) 
   {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_addresult);
    database = new Database(this);
    sp1 = findViewById(R.id.spinner1);
    sp2 = findViewById(R.id.spinner2);
    sp3 = findViewById(R.id.spinner3);
    sp4 = findViewById(R.id.spinner4);
    sp5 = findViewById(R.id.spinner5);
    sp6 = findViewById(R.id.spinner6);
    sp7 = findViewById(R.id.spinner7);
    eng = findViewById(R.id.edsub1);
    urdu = findViewById(R.id.edsub2);
    math = findViewById(R.id.edsub3);
    bio = findViewById(R.id.edsub4);
    phy = findViewById(R.id.edsub5);
    comp = findViewById(R.id.edsub6);
    chem = findViewById(R.id.edsub7);
    r = findViewById(R.id.edroll);
    ArrayAdapter ad = new ArrayAdapter(this, 
    android.R.layout.simple_dropdown_item_1line, 
    subject);  

//Here I have pass Subject string to the adapter

    sp1.setAdapter(ad);
    sp2.setAdapter(ad);
    sp3.setAdapter(ad);
    sp4.setAdapter(ad);
    sp5.setAdapter(ad);
    sp6.setAdapter(ad);
    sp7.setAdapter(ad);
    sp1.setAdapter(ad);

// this on click listen that give the position of

 the subject that user choose

 sp1.setOnItemSelectedListener(new 
 AdapterView.OnItemSelectedListener() {
 public void onItemSelected(AdapterView<?> 
 parent, View view, int pos, long id) {
 String item 
 =sp1.getSelectedItem().toString();
            Toast.makeText(Addresult.this, item, 
 Toast.LENGTH_SHORT).show();
        }
        public void 
  onNothingSelected(AdapterView<?> parent) {
        }
    });
} 

//this is my add result funtions

    public void addResult(View v) {
    String English = eng.getText().toString();
    String Urdu = urdu.getText().toString();
    String Math = math.getText().toString();
    String Bio = bio.getText().toString();
    String Physics = phy.getText().toString();
    String Computer = comp.getText().toString();
    String Chemistry = chem.getText().toString();
    String rol = r.getText().toString();
    if (English.equals("") || Urdu.equals("") || 
    Math.equals("") || Bio.equals("") || 
    Physics.equals("") || Computer.equals("") || 
    Chemistry.equals("") || rol.equals(""))
    Toast.makeText(this, "Please fill all 
    field first", Toast.LENGTH_SHORT).show();
    else {
        int roll = Integer.parseInt(rol);
        resultmodel result = new 
        resultmodel(English, Urdu, Math, Bio, 
        Chemistry, 
        Physics, Computer, roll);
        int i=database.insertions(result);

//I want to get value of spinner selected subject because I have store this subject by sequence into the SQLite

database where I have created all subject by a sequence } }
} //this is my table where i have keep sequence of subjecs

tatic String subject1=     "English";
private static String subject2=     "Urud";
private static String subject3=     "Math";
private static String subject4=     "Biology";
private static String subject5=     "Chemistry";
private static String subject6=     "Physics";
private static String subject7=     "Computer";

// if I use each spinner for every select subject it is most time consuming approach can any other method is to achive my goalenter image description here

CodePudding user response:

If the subject list is static and you want to get marks for all the subjects you dont need spinner, you can use recylerview to create dynamic list with each item containing subject textview and edittext to enter mark.

Else if user can enter only selected subjects, you can keep a single spinner and edittext at top with add button,So that user can select a subject and enter marks and click add button. In add button click event, you should store the subject along with marks in a separate array. Then finally can insert all the entered values.

CodePudding user response:

If you have a limited number of known subject fields, then there is no point in giving an option for selecting subjects, as you already need to enter marks of all subjects, here you can add textView for the subject's title and their corresponding editText for their marks.

If you have a use case, where users have to select some subjects out of a given list (not necessarily all of them), there you can use spinners for selecting subjects.

For selecting a spinner option, using the saved value from the database

spinner.setSelection(itemPosition);

itemPosition is an int and it is the position of the item in the adapter list. For your use case, you have to save the position of the item in the database, not the name.

  • Related