我已经尝试了许多教程,没有用来解决这个错误面临。 的错误是微调充满了数据,但一旦我选择任何然后它变得空白所选项目 这里有两个图片: 微调工作不正常
这里是代码片段:
package com.school;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class AdminAddAbsence extends Activity implements OnClickListener{
\t
\t Spinner spinnerStudentId;
\t Button bAddStudentAbsence,bBack;
\t EditText etAdminAddAbsenceDate;
\t DatePicker dpAdminAddAbsenceDate;
\t JSONParser jsonParser = new JSONParser();
\t ArrayList<HashMap<String, String>> students;
\t ArrayList<String>items;
\t ArrayList<Student>studentsList;
\t @Override
\t protected void onCreate(Bundle savedInstanceState) {
\t \t // TODO Auto-generated method stub
\t \t super.onCreate(savedInstanceState);
\t \t setContentView(R.layout.admin_add_absence);
\t \t initVars();
\t }
\t private void initVars() {
\t \t // TODO Auto-generated method stub
\t \t bAddStudentAbsence = (Button) findViewById(R.id.bAdminAddAbsenceAdd);
\t \t bAddStudentAbsence.setOnClickListener(this);
\t \t bBack = (Button) findViewById(R.id.bAdminAddAbsenceBack);
\t \t bBack.setOnClickListener(this);
\t \t spinnerStudentId = (Spinner) findViewById(R.id.spinnerAdminAddAbsenceStudentId);
\t \t etAdminAddAbsenceDate = (EditText) findViewById(R.id.etAdminAddAbsenceDate);
\t \t dpAdminAddAbsenceDate = (DatePicker) findViewById(R.id.dpAdminAddAbsenceDate);
\t \t
\t \t students = new ArrayList<HashMap<String,String>>();
\t \t new getStudents().execute();
\t \t
\t \t SimpleAdapter sAdap;
\t \t sAdap = new SimpleAdapter(AdminAddAbsence.this, students, R.layout.spinner_details,
\t \t new String[] {"studentID", "studentName"}, new int[] {R.id.tvSpinnerDetailsStudentId, R.id.tvSpinnerDetailsStudentName});
\t \t spinnerStudentId.setAdapter(sAdap);
\t \t
\t \t spinnerStudentId.setOnItemSelectedListener(new OnItemSelectedListener() {
\t \t \t public void onItemSelected(AdapterView<?> arg0, View arg1,
\t \t \t \t \t int position, long id) {
\t \t \t \t // TODO Auto-generated method stub
\t \t \t \t String studentID = students.get(position).get("studentID")
\t \t \t \t \t \t .toString();
\t \t \t \t \t \t String sName = students.get(position).get("studentName")
\t \t \t \t \t \t .toString();
\t \t \t \t \t \t Toast.makeText(AdminAddAbsence.this, studentID + " "+sName, Toast.LENGTH_LONG).show();
\t \t \t }
\t \t \t public void onNothingSelected(AdapterView<?> arg0) {
\t \t \t \t // TODO Auto-generated method stub
\t \t \t \t Toast.makeText(AdminAddAbsence.this, "Nothing selected", Toast.LENGTH_LONG).show();
\t \t \t }
\t \t });
\t \t
\t }
\t public void onClick(View v) {
\t \t // TODO Auto-generated method stub
\t \t switch(v.getId()){
\t \t case R.id.bAdminAddAbsenceBack:
\t \t \t finish();
\t \t \t break;
\t \t }
\t }
\t class getStudents extends AsyncTask<String, String, String>{
\t \t @Override
\t \t protected String doInBackground(String... params) {
\t \t \t // TODO Auto-generated method stub
\t \t \t
\t \t \t \t \t // TODO Auto-generated method stub
\t \t \t \t \t try {
\t \t \t \t \t List<NameValuePair>param = new ArrayList<NameValuePair>();
\t \t \t \t \t param.add(new BasicNameValuePair("OPERATION", "GET_STUDENTS"));
\t \t \t \t \t JSONObject json = jsonParser.makeHttpRequest(Utils.OPERATIONS_URL, "POST", param);
\t \t \t \t \t
\t \t \t \t \t \t JSONArray jsonArray = json.getJSONArray("database");
\t \t \t \t \t \t HashMap<String, String>map;
\t \t \t \t \t \t for(int i=0;i<jsonArray.length();i++){
\t \t \t \t \t \t \t map = new HashMap<String, String>();
\t \t \t \t \t \t \t JSONObject jsonObject = jsonArray.optJSONObject(i);
\t \t \t \t \t \t \t int studentID = jsonObject.getInt("idStudent");
\t \t \t \t \t \t \t String studentName = jsonObject.getString("name");
\t \t \t \t \t \t \t map.put("studentID", studentID+"");
\t \t \t \t \t \t \t map.put("studentName", studentName);
\t \t \t \t \t \t \t students.add(map);
\t \t \t \t \t \t }
\t \t \t \t \t } catch (JSONException e) {
\t \t \t \t \t \t // TODO Auto-generated catch block
\t \t \t \t \t \t e.printStackTrace();
\t \t \t \t \t }
\t \t \t \t \t
\t \t \t \t
\t \t \t
\t \t \t return null;
\t \t }
\t \t
\t }
\t class Student {
\t \t // Okay, full acknowledgment that public members are not a good idea, however
\t \t // this is a Spinner demo not an exercise in java best practices.
\t \t public int id = 0;
\t \t public String name = "";
\t \t // A simple constructor for populating our member variables for this tutorial.
\t \t public Student(int _id, String _name)
\t \t {
\t \t id = _id;
\t \t name = _name;
\t \t }
\t \t
\t \t public int getId() {
\t \t \t return id;
\t \t }
\t \t public void setId(int id) {
\t \t \t this.id = id;
\t \t }
\t \t public String getName() {
\t \t \t return name;
\t \t }
\t \t public void setName(String name) {
\t \t \t this.name = name;
\t \t }
\t \t // The toString method is extremely important to making this class work with a Spinner
\t \t // (or ListView) object because this is the method called when it is trying to represent
\t \t // this object within the control. If you do not have a toString() method, you WILL
\t \t // get an exception.
\t \t public String toString()
\t \t {
\t \t return("("+name +")");
\t \t }
\t \t }
}
在此先感谢
当你不选择,微调是否为空或不? – xxxzhi
修改代码之前是的,它是空白的,但是在我在下面的答案中修改我的代码之后,其默认选择是返回数组的第一个选择 –