java - Android Spinner selected item not showing

538

i got the items from the database

Here's the java code

package com.example.androidtablayout;

public class Leave extends Activity implements OnItemSelectedListener{
Button btnSubmitLeave,btnLogout;
SessionManager session;
JSONParser jsonParser = new JSONParser();
Spinner mySpinner;
ProgressDialog mProgressDialog;
JSONArray jsonarray;

ArrayList<String> leavetype;
String username;
EditText inputStart,inputEnd,inputReason;
final Calendar myCalendar = Calendar.getInstance();

private String url_leavetype = "http://10.0.3.2/sunshine-ems/get_leavetype.php";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.leave);

    // CHECK TO LOGIN SESSION
    session = new SessionManager(getApplicationContext());
    session.checkLogin();
    username = session.getUsername();


    inputReason = (EditText) findViewById(R.id.inputReason);
    inputStart = (EditText) findViewById(R.id.inputStart);
    inputEnd = (EditText) findViewById(R.id.inputEnd);
    btnSubmitLeave = (Button) findViewById(R.id.btnSubmitLeave);


    //spinner
    mySpinner= (Spinner) findViewById(R.id.spinnerLeaveType);
    leavetype = new ArrayList<String>();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, leavetype);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    new getLeaveTypes().execute();
    mySpinner.setAdapter(adapter);
    mySpinner.setOnItemSelectedListener(this);

}
private class getLeaveTypes extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(Leave.this);
        mProgressDialog.setTitle("Getting Leave types");
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {

        int success;

        List<NameValuePair> params1 = new ArrayList<NameValuePair>();           
        try {

            params1.add(new BasicNameValuePair("username", username));          
            JSONObject json = jsonParser.makeHttpRequest(url_leavetype, "GET",params1);

            // full json response
            Log.d("Get Leave types", json.toString());

            // json success element
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {

                JSONArray type = json.getJSONArray("leavetypes");                        

                for (int i = 0; i < type.length(); i++) {
                    JSONObject catObj = (JSONObject) type.get(i);
                    leavetype.add(catObj.optString("leave_name"));
                }

            } else {
                Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        mProgressDialog.dismiss();
    }

}

public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    Toast.makeText(
            getApplicationContext(),
                    parent.getItemAtPosition(position).toString() + " Selected" ,
            Toast.LENGTH_LONG).show();

}


public void onNothingSelected(AdapterView<?> arg0) {      
}
}

That data from the database is there, the spinner list is showing, but when i clicked an item, it's not showing on that spinner. I hope someone canhelp me here. Thank you

312

Answer

Solution:

Add thisadapter.notifyDataSetChanged() in onPostExecute() method:

protected void onPostExecute(String file_url) {
    // dismiss the dialog once product deleted
    adapter.notifyDataSetChanged();
    mProgressDialog.dismiss();

}

People are also looking for solutions to the problem: php - Apache connected but still got a strange message when I start server

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.