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

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 docker环境配置
  • ¥20 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现