0

I have a spinner item and the value has been inserted to SQLite.Now I want to retrieve the item out to another spinner which is in Update.java.

Information.java

 public void addItemsOnSpinner2() {
        spinner2 = (Spinner) findViewById(R.id.spinner2);
        List<String> list = new ArrayList<String>();
        list.add("Sunny");
        list.add("Cloudy");
        list.add("Rainy");
        list.add("Thunderstorm");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(Information.this, android.R.layout.simple_spinner_dropdown_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter);

Update.java

  public void addWeather() {
    weather3 = (Spinner) findViewById(R.id.spinner5);
    List<String> list = new ArrayList<String>();
    list.add("Sunny");
    list.add("Cloudy");
    list.add("Rainy");
    list.add("Thunderstorm");
    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);
    weather3.setAdapter(adapter);
    String Weather = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Weather));
    weather3.setText(Weather);
}

Of course this method doesn't work and the setText cannot be resolved. I've been searching the solution for a long time but it seems like not much information can be found . Any idea or suggestions would be greatly appreciated.

Edited

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbHelper = new MyDatabaseHelper(this);
        setContentView(R.layout.updatepage);
        final String name = getIntent().getExtras().getString("name1");
        final String date=getIntent().getExtras().getString("date1");
        final String ID = getIntent().getExtras().getString("ID");
        addWeather();
        RetrievePage(name,date,ID);
    }

     public void addWeather() {
            weather3 = (Spinner) findViewById(R.id.spinner5);
            String[] arr = new String[]{"Sunny","Cloudy","Rainy","Thunderstorm"};
            List<String> list = new ArrayList<String>();
            String weather = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Weather));
            list.add(weather);
            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);
            weather3.setAdapter(adapter);
        }

 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);

        final EditText subC3 = (EditText) findViewById(R.id.editText17);
        final EditText noP = (EditText) findViewById(R.id.editText18);
        final EditText noH = (EditText) findViewById(R.id.editText19);
        final Spinner poject3 = (Spinner) findViewById(R.id.spinner8);


        database = dbHelper.getWritableDatabase();
        c = database.rawQuery("SELECT i.Weather, i.Status,w.Subcontractors, w.NumberOfPerson, w.NumberOfHours FROM Information i LEFT JOIN WorkForce w ON w.TInfo_id = i._id WHERE i.Name = ? AND i._id= ? ",
                new String[]{String.valueOf(name2),String.valueOf(id2)}, null);
        if (c != null) {
            while (c.moveToNext()) {
                Info I = new Info();
                Details WD = new Details();
                Force WF = new Force();
                String Weather = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Weather));
                String Status = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Status));
                String SubC = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Subcontractors));
                String NoP = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.NumberOfPerson));
                String NoH = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.NumberOfHours));
                I.setWeather(Weather);
                I.setStatus(Status);
                WF.setSubcontractors(SubC);
                WF.setNoOfPerson(NoP);
                WF.setNoOfHours(NoH);
                subC3.setText(SubC);
                noP.setText(NoP);
                noH.setText(NoH);

            }

        }
        c.close();

Error

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getColumnIndexOrThrow(java.lang.String)' on a null object reference
Hoo
  • 1,806
  • 7
  • 33
  • 66
  • Do the same thing you've done for `spinner2` except that you add only the `Weather` `String` to the `list` `List `. – Titus Oct 17 '15 at 16:36
  • Do you mean the ArrayAdapter adapter ? – Hoo Oct 17 '15 at 16:38
  • Yes, create an adapter and add only `Weather` to the list that you pass to the adapter's constructor. – Titus Oct 17 '15 at 16:40
  • where should I add the weather3.setText? Please see the edited post – Hoo Oct 17 '15 at 16:57
  • `Spinner` doesn't have a `setText(...)` method, you will have to set its content using an adapter, just like you've did for the `spinner2` `Spinner` – Titus Oct 17 '15 at 17:01
  • Can you work it out for me please... – Hoo Oct 17 '15 at 17:06
  • I'm not sure I understand exactly what you want to do. Do you wan't to display only the content of `Weather` in the second spinner ? – Titus Oct 17 '15 at 17:07
  • the spinner item in Information.java already inserted in SQlite. Now I want to retrieve it and display to Update.java which has same spinner item in Information – Hoo Oct 17 '15 at 17:09
  • So, you want to select the same item in `weather3` as it is selected in `spinner2` ? – Titus Oct 17 '15 at 17:13
  • ya, it will show the same item that selected in spinner 2 ...does it sounds possible? – Hoo Oct 17 '15 at 17:15

1 Answers1

0

I'm not sure I understand exactly what you're trying to achieve but if you want to display the item selected in spinner2 in the weather3 spinner, you can do something like this:

    public void addWeather() {
        weather3 = (Spinner) findViewById(R.id.spinner5);
        String[] arr = new String[]{"Sunny","Cloudy","Rainy","Thunderstorm"};
        List<String> list = new ArrayList<String>();
        String weather = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Weather));
        list.add(weather);
        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);
        weather3.setAdapter(adapter);
    }

The idea is to add the item that you want to be displayed as the first item in the ArrayList.

Titus
  • 22,031
  • 1
  • 23
  • 33
  • It seems like the answer I looking for. however, I get Cursor.getColumnIndexOrThrow(java.lang.String)' on a null object reference.... – Hoo Oct 17 '15 at 17:30
  • @Hoo It seems that `c` is `null`. – Titus Oct 17 '15 at 17:33
  • Need to move the String Weather to retrievePage()?? See the edited post – Hoo Oct 17 '15 at 17:36
  • @Hoo You can create the spinner's adapter inside the `RetrievePage(...)` method. – Titus Oct 17 '15 at 17:39
  • Did you mean move everything to RetrievePage? – Hoo Oct 17 '15 at 17:40
  • @Hoo , yes, you can do that. I'm still not sure what you're trying to do since there can be multiple `Weather` values if your `SQL` statement returns multiple rows. Also, in the `while` loop, you're creating new objects but you're not using them in any way. – Titus Oct 17 '15 at 17:45
  • Omg..It works like charm...just move the string weather to retrievePage, and then pass it to addWeather...Thanks :) – Hoo Oct 17 '15 at 17:48
  • I will use those object later :) – Hoo Oct 17 '15 at 17:52
  • Can you help me solve this? http://stackoverflow.com/questions/33199631/java-lang-nullpointerexception-attempt-to-invoke-virtual-method-java-lang-stri – Hoo Oct 18 '15 at 16:00