drqxfmfa804578 2015-02-03 20:44
浏览 146
已采纳

Android Spinner选择的项目未显示

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

  • 写回答

1条回答 默认 最新

  • donglu9445 2015-02-03 20:52
    关注

    Add this adapter.notifyDataSetChanged() in onPostExecute() method:

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        adapter.notifyDataSetChanged();
        mProgressDialog.dismiss();
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog