【发布时间】:2015-09-20 20:39:28
【问题描述】:
我尝试了许多教程,但都无法解决我面临的这个错误。 错误是微调器充满了数据,但是一旦我选择任何一个,它就会变成空白的选定项目 这是两张照片:
这里是代码 sn-p :
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 +")");
}
}
}
提前致谢
【问题讨论】:
-
不选择时,Spinner是否为空?
-
在修改代码之前是空白的,但是在我在下面的答案中修改了我的代码之后,它的默认选择是返回数组的第一个选择
标签: android json arraylist spinner