旋转器工作不正常

时间:2015-09-20 20:39:29

标签: android json arraylist spinner

我尝试了很多教程,没有用来解决这个错误。 错误是微调器充满了数据但是一旦我选择了任何一个,它就会得到空白的选定项目 这是两个图片: enter image description here

enter image description here

以下是代码段:

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{
	
	Spinner spinnerStudentId;
	Button bAddStudentAbsence,bBack;
	EditText etAdminAddAbsenceDate;
	DatePicker dpAdminAddAbsenceDate;
	JSONParser jsonParser = new JSONParser();
	ArrayList<HashMap<String, String>> students;
	ArrayList<String>items;
	ArrayList<Student>studentsList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.admin_add_absence);
		initVars();
	}

	private void initVars() {
		// TODO Auto-generated method stub
		bAddStudentAbsence = (Button) findViewById(R.id.bAdminAddAbsenceAdd);
		bAddStudentAbsence.setOnClickListener(this);
		bBack = (Button) findViewById(R.id.bAdminAddAbsenceBack);
		bBack.setOnClickListener(this);
		spinnerStudentId = (Spinner) findViewById(R.id.spinnerAdminAddAbsenceStudentId);
		etAdminAddAbsenceDate = (EditText) findViewById(R.id.etAdminAddAbsenceDate);
		dpAdminAddAbsenceDate = (DatePicker) findViewById(R.id.dpAdminAddAbsenceDate);
		
		students = new ArrayList<HashMap<String,String>>();
		new getStudents().execute();
		
		 SimpleAdapter sAdap;
		 sAdap = new SimpleAdapter(AdminAddAbsence.this, students, R.layout.spinner_details,
		 new String[] {"studentID", "studentName"}, new int[] {R.id.tvSpinnerDetailsStudentId, R.id.tvSpinnerDetailsStudentName});
		 spinnerStudentId.setAdapter(sAdap);
		 
		 spinnerStudentId.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long id) {
				// TODO Auto-generated method stub
				String studentID = students.get(position).get("studentID")
						 .toString();
						 String sName = students.get(position).get("studentName")
						 .toString();
						 Toast.makeText(AdminAddAbsence.this, studentID + " "+sName, Toast.LENGTH_LONG).show();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(AdminAddAbsence.this, "Nothing selected", Toast.LENGTH_LONG).show();
			}
		});
		
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.bAdminAddAbsenceBack:
			finish();
			break;
		}
	}
	class getStudents extends AsyncTask<String, String, String>{

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			
					// TODO Auto-generated method stub
					try {
					List<NameValuePair>param = new ArrayList<NameValuePair>();
					param.add(new BasicNameValuePair("OPERATION", "GET_STUDENTS"));
					JSONObject json = jsonParser.makeHttpRequest(Utils.OPERATIONS_URL, "POST", param);
					
						JSONArray jsonArray = json.getJSONArray("database");
						HashMap<String, String>map;
						for(int i=0;i<jsonArray.length();i++){
							map = new HashMap<String, String>();
							JSONObject jsonObject = jsonArray.optJSONObject(i);
							int studentID = jsonObject.getInt("idStudent");
							String studentName = jsonObject.getString("name");
							map.put("studentID", studentID+"");
							map.put("studentName", studentName);
							students.add(map);
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				
			
			return null;
		}
		
	}
	 class Student {

		// Okay, full acknowledgment that public members are not a good idea, however
		// this is a Spinner demo not an exercise in java best practices.
		public int id = 0;
		public String name = "";

		// A simple constructor for populating our member variables for this tutorial.
		public Student( int _id, String _name)
		{
		    id = _id;
		    name = _name;
		}
		

		public int getId() {
			return id;
		}


		public void setId(int id) {
			this.id = id;
		}


		public String getName() {
			return name;
		}


		public void setName(String name) {
			this.name = name;
		}


		// The toString method is extremely important to making this class work with a Spinner
		// (or ListView) object because this is the method called when it is trying to represent
		// this object within the control.  If you do not have a toString() method, you WILL
		// get an exception.
		public String toString()
		{
		    return( "("+name +")");
		}
		}

}

提前致谢

2 个答案:

答案 0 :(得分:0)

让我们在onItemSelected方法

中尝试一下
spinnerStudentId.setSelection(position);

答案 1 :(得分:0)

我找到了解决方案: 解决这个问题的解决方案是我将适配器本身放在AsyncTask类的doInBackground方法中,但放在内部runOnUIThread中,因为除了这个匿名类之外,你将无法在AsyncTask类的UI上进行修改。 所以这里是修改过的AsyncTask类

class getStudents extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub

			// TODO Auto-generated method stub
			try {
				List<NameValuePair> param = new ArrayList<NameValuePair>();
				param.add(new BasicNameValuePair("OPERATION", "GET_STUDENTS"));
				JSONObject json = jsonParser.makeHttpRequest(
						Utils.OPERATIONS_URL, "POST", param);

				JSONArray jsonArray = json.getJSONArray("database");
				HashMap<String, String> map;
				for (int i = 0; i < jsonArray.length(); i++) {
					map = new HashMap<String, String>();
					JSONObject jsonObject = jsonArray.optJSONObject(i);
					int studentID = jsonObject.getInt("idStudent");
					String studentName = jsonObject.getString("name");
					map.put("studentID", studentID + "");
					map.put("studentName", studentName);
					students.add(map);
					
				}
				runOnUiThread(new Runnable() {
					
					public void run() {
						// TODO Auto-generated method stub
						SimpleAdapter sAdap = new SimpleAdapter(AdminAddAbsence.this, students,
								R.layout.spinner_details, new String[] { "studentID",
										"studentName" }, new int[] {
										R.id.tvSpinnerDetailsStudentId,
										R.id.tvSpinnerDetailsStudentName });
						spinnerStudentId.setAdapter(sAdap);
						spinnerStudentId
								.setOnItemSelectedListener(new OnItemSelectedListener() {

									public void onItemSelected(AdapterView<?> parent, View view,
											int position, long id) {
										// TODO Auto-generated method stub
										String studentID = students.get(position)
												.get("studentID").toString();
										String sName = students.get(position)
												.get("studentName").toString();
										etAdminAddAbsenceDate.setText(studentID);
										Toast.makeText(AdminAddAbsence.this,
												parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG)
												.show();
									}

									public void onNothingSelected(AdapterView<?> arg0) {
										// TODO Auto-generated method stub
										Toast.makeText(AdminAddAbsence.this,
												"Nothing selected", Toast.LENGTH_LONG).show();
									}
								});
					}
				});
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return null;
		}

	}