Read 3 selected item in the same time
Asked Answered
C

2

9

How to read 3 spinner selected item in the same time? I have spinner1,2,3 in WorkDetails.java and their value has been inserted into SQLite. In my UpdatePage.java, I want to retrieve the selected item out and the selected item should be displayed first in spinner project1,project2 and project3.

UpdatePage.java

public void RetrievePage(String name,String date, String id) {
    final String name2 = name;
    final String date2=date;
    final String id2 = id;
    final EditText name3 = (EditText) findViewById(R.id.editText9);
    final EditText date3 = (EditText) findViewById(R.id.editText12);
    name3.setText(name2);
    date3.setText(date2);

    database = dbHelper.getWritableDatabase();
    c = database.rawQuery(
        "SELECT i.Weather, i.Status,w.Subcontractors, w.NumberOfPerson,"+
        "w.NumberOfHours, wd.Project, wd.WorkDescription, wd.Per,"+
        "wd.TimeIn, wd.TimeOut FROM Information i LEFT JOIN WorkForce w "+
        "ON w.TInfo_id = i._id "+
        "LEFT JOIN WorkDetails wd ON wd.Twf_id=w._id "+
        "WHERE i.Name = ? AND i._id= ? ",
        new String[]{String.valueOf(name2),String.valueOf(id2)}, null);
    if (c != null) {
        while (c.moveToNext()) {
            Details WD = new Details();
            String Project11 = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project));
            String Project22  =c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project));
            String Project33  =c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project));

            addProject1(Project11);
            addProject2(Project22);
            addProject3(Project33);
            WD.setProject(Project11);
            WD.setProject(Project22);
            WD.setProject(Project33);
        }
    }
    c.close();
}

public void addProject1(String l) {
    project1 = (Spinner) findViewById(R.id.spinner8);
    String[] arr = new String[]{"1","2","3"};
    List<String> list = new ArrayList<String>();
    String project11 = l;
    list.add(project11);
    for (String s : arr) {
        if (!list.contains(s)) {
            list.add(s);
        }
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdatePage.this, android.R.layout.simple_spinner_dropdown_item, list);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    project1.setAdapter(adapter);
}

public void addProject2(String d) {
    project2 = (Spinner)findViewById(R.id.spinner9);
    String[] arr = new String[]{"1","2","3"};
    List<String> list = new ArrayList<String>();
    String project22 = d;
    list.add(project22);
    for (String s:arr) {
        if(!list.contains(s)) {
            list.add(s);
        }
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdatePage.this, android.R.layout.simple_spinner_dropdown_item, list);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    project2.setAdapter(adapter);
}

public void addProject3(String e) {
    project3 = (Spinner)findViewById(R.id.spinner13);
    String[] arr = new String[]{"1","2","3"};
    List<String> list = new ArrayList<String>();
    String project33 = e;
    list.add(project33);
    for (String s:arr) {
        if (!list.contains(s)) {
            list.add(s);
        }
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdatePage.this, android.R.layout.simple_spinner_dropdown_item, list);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    project3.setAdapter(adapter);
}

Assume spinner1 holds value 1, spinner2 holds 2 and spinner3 holds 3. When come to updatePage, spinner project1 should display 1 first, project2 display 2 and so on. But the problem now is project1,2,and 3 display value 1 first...how can I achieve this?

Cordell answered 21/10, 2015 at 4:29 Comment(14)
hi, Let me clarify something here first, so what you exactly want is that when you change the value of spinner the 1st value shows into 1st spinner, 2nd value shows into 2nd spinner and 3rd value shows into 3rd spinner. is that right?Tobar
I sugessted you using this questionsMt
@HardikChauhan Exactly..:)Cordell
@Cordell : and are you using the same array for assign value in all three spinner?Tobar
@hardik did you mean the name?Cordell
@Cordell : i mean that which value you want to show and get from the spinner? which array of list or something?Tobar
@HardikChauhan It depends on what value was inserted into SQLite. If spinner 1 was inserted value 1, then in project1 should display value 1 first..Cordell
so if the value is same for all spinner you can set same.Tobar
@HardikChauhan the item in the list are same but the item selected might not the sameCordell
Yes so if the list is same, so when you select on value from array get the index of that value and set the same for other spinner.Tobar
Let us continue this discussion in chat.Tobar
Hi John, let's try to decide your problem. Go to chat for clarify what you need. How your program are working at this moment?Goldberg
Did you get a resolution for this?Selfsacrifice
@ManuSunny ya, I already got it. The spinner display the same item is because of this String Project11 = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project)); String Project22 =c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project)); String Project33 =c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project));. I should add c.moveToNext() between themCordell
J
4

You can try a little bit of logic for this purpose

Take three values from the database. Let them be

selected_value_of_spinner1,selected_value_of_spinner2,selected_value_of__spinner3

Then define your method getPosition as below

public int getPosition(Spinner your_spinner, String string_value){

    int position = 0;

    for (int i=0;i<your_spinner.getCount();i++){
        if (your_spinner.getItemAtPosition(i).equals(string_value)){
            position = i;
        }
    }
    return position;
    }

The show the values got from the database in your spinners like following

your_spinner1.setSelection(getPosition(your_spinner1, selected_value_of_spinner1));
your_spinner2.setSelection(getPosition(your_spinner2, selected_value_of_spinner2));
your_spinner3.setSelection(getPosition(your_spinner3, selected_value_of_spinner3));

Basically what happens here is

  1. You get the value from the database

  2. You match the value got from database in your spinner to get its position in spinner by getPosition() method.

  3. Set the spinner value according to the position you get from step 2 by setSelection(position) method.

Justificatory answered 21/10, 2015 at 5:17 Comment(7)
I have 3 spinner, which spinnner should I write for the (your_spinner)Cordell
Do it three times for 3 spinners @CordellJustificatory
how abou the string_value?Cordell
I have edited the answer for your better understanding. try now. @CordellJustificatory
I still can't get it..what value should I put in selected_value_of_spinnerCordell
in your updatepage.java you get the values stored from the database right? and those values are selected values for the spinners right? so just pass those three selected values to the function. @CordellJustificatory
Let us continue this discussion in chat.Cordell
C
1

Since you are having 3 Spinner items then you can do this:

project1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedItem1 = list.get(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

project2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedItem2 = list.get(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

project3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedItem3 = list.get(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

and declare the:

String selectedItem1;
String selectedTtem2;
String selectedItem3;

as global variables. Hope it helps!!!

Coaptation answered 29/11, 2015 at 16:50 Comment(1)
hey, thanks for your answer. I already got the answer . The spinner display the same item is because of this String Project11 = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project)); String Project22 =c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project)); String Project33 =c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project));. I should add c.moveToNext() between themCordell

© 2022 - 2024 — McMap. All rights reserved.