Android Spinner选择的项目未显示

时间:2015-02-03 20:44:05

标签: java php android mysql json

我从数据库中获取了项目

这是java代码

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) {      
}
}

来自数据库的数据就在那里,微调器列表正在显示,但是当我点击一个项目时,它没有显示在该微调器上。 我希望有人能在这里帮助我。谢谢

1 个答案:

答案 0 :(得分:3)

在onPostExecute()方法中添加此adapter.notifyDataSetChanged()

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

}