Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read 3 selected item in the same time

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?

like image 879
John Avatar asked Oct 21 '15 04:10

John


2 Answers

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.

like image 99
Zahan Safallwa Avatar answered Nov 15 '22 15:11

Zahan Safallwa


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!!!

like image 1
Kostas Drak Avatar answered Nov 15 '22 17:11

Kostas Drak