【发布时间】:2017-01-19 17:07:14
【问题描述】:
我正在将 JSON 解析为微调器,但我没有在微调器中获取所有数据, 我正在 Spinner 中获取最后一个关键数据
下面是 onPostExecute 方法。从我通过 Intent 将数组发送到下一个类的地方,即 Put_Credentials.java ..
这是我的完整 JSON:
[
[
{
"User_Id": "PANKAJ",
"Password": "31184555",
"teacher_id": "24",
"teacher_name": "MR. PANKAJ SINGH",
"msg": "Successfully Login"
}
],
[
{
"Batch_Id": "1",
"Batch": "2016-21"
},
{
"Batch_Id": "2",
"Batch": "2015-20"
},
{
"Batch_Id": "3",
"Batch": "2014-19"
},
{
"Batch_Id": "4",
"Batch": "2013-18"
},
{
"Batch_Id": "5",
"Batch": "2012-17"
},
{
"Batch_Id": "6",
"Batch": "2014-17"
},
{
"Batch_Id": "7",
"Batch": "2015-18"
},
{
"Batch_Id": "8",
"Batch": "2016-19"
}
],
[
{
"Section_Id": "1",
"Section_Name": "A"
},
{
"Section_Id": "2",
"Section_Name": "B"
},
{
"Section_Id": "3",
"Section_Name": "C"
},
{
"Section_Id": "4",
"Section_Name": "D"
},
{
"Section_Id": "5",
"Section_Name": "E"
}
],
[
{
"subject_id": "1",
"subject_name": "English-I"
},
{
"subject_id": "2",
"subject_name": "English III"
},
{
"subject_id": "3",
"subject_name": "Jurisprudence"
},
{
"subject_id": "4",
"subject_name": "Company Law"
},
{
"subject_id": "5",
"subject_name": "Law of Evidence"
},
{
"subject_id": "6",
"subject_name": "Sociology-I"
},
{
"subject_id": "7",
"subject_name": "Hindi -I"
}
]
]
______________++++++++++++++++++++++++++++++++++++++__________________
这是我的 Login_Activity。
这里的结果是完整的数组(如上所示),从 JSON 中找到.. 从这里我将数据发送到 PutCredentials.java 类
@Override
protected void onPostExecute(String result) {
try {
if (progress != null) {
progress.dismiss();
}
JSONArray jsonArray = new JSONArray(result);
jsonArrayTeacherName = jsonArray.getJSONArray(0);
jsonArrayBatch = jsonArray.getJSONArray(1);
jsonArraySection = jsonArray.getJSONArray(2);
jsonArraySubject = jsonArray.getJSONArray(3);
JSONObject jsonResponse = jsonArrayTeacherName.getJSONObject(0);
teachername = jsonResponse.getString("teacher_name");
batch = jsonArrayBatch.toString();
section = jsonArraySection.toString();
subject = jsonArraySubject.toString();
`Intent i = new Intent(Login_activity.this, PutCredentials.class);
i.putExtra("BATCH_ARRAY", jsonArrayBatch.toString());
i.putExtra("SECTION_ARRAY", jsonArraySection.toString());
i.putExtra("SUBJECT_ARRAY", jsonArraySubject.toString());
i.putExtra("Password",Password);
i.putExtra("User_Id", Idcardno);
i.putExtra("login_id", loginId);
i.putExtra("teacher_name", teachername);
startActivity(i);
}
catch (JSONException e) {
Log.e("MainActivity", "unexpected JSON exception", e);
}
// Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
}`
_____________________________+++++___________________________________
这里我从登录活动中获取数据。
PutCredentials.Java:
public class PutCredentials extends Activity {
ProgressDialog progress;
TextView TVTeacherName;
String Password, Idcardno, loginId, teachername, teacherid;
Button submit;
Spinner batchSpinner, sectionSpinner, subjectSpinner;
ArrayList<String> batchlist;
ArrayList<String> sectionlist;
ArrayList<String> subjectlist;
String jsonArrayForBatch;
String jsonArrayForSection;
String jsonArrayForSubject;
String batchdata, sectiondata, subjectdata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_put_credentials);
TVTeacherName = (TextView) findViewById(R.id.tv_teachername);
submit = (Button) findViewById(R.id.button_submit);
batchSpinner = (Spinner) findViewById(R.id.batch_spinner);
sectionSpinner = (Spinner) findViewById(R.id.section_spinner);
subjectSpinner = (Spinner) findViewById(R.id.subject_spinner);
Intent intent = getIntent();
jsonArrayForBatch = intent.getStringExtra("BATCH_ARRAY");
jsonArrayForSection = intent.getStringExtra("SECTION_ARRAY");
jsonArrayForSubject = intent.getStringExtra("SUBJECT_ARRAY");
Password = intent.getExtras().getString("Password");
Idcardno = intent.getExtras().getString("User_Id");
loginId = intent.getExtras().getString("login_id");
teachername = intent.getExtras().getString("teacher_name");
getBatchSpinner();
getSectionSpinner();
getSubjectSpinner();
TVTeacherName.setText(teachername);
dateView = (TextView) findViewById(R.id.date);
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
showDate(year, month+1, day);
});
}
private void getBatchSpinner (){
try {
JSONArray jArray = new JSONArray(jsonArrayForBatch);
JSONObject j = null;
for (int i = 0; i < jArray.length(); i++) {
j = jArray.getJSONObject(i);
if (j != null) {
batchdata = j.optString("Batch");
}
batchlist = new ArrayList<String>();
batchlist.add(batchdata);
}
batchSpinner
.setAdapter(new ArrayAdapter<String>(PutCredentials.this,
android.R.layout.simple_spinner_dropdown_item,
batchlist));
} catch (JSONException e) {
e.printStackTrace();
}
}
private void getSectionSpinner (){
try {
JSONArray jArray = new JSONArray(jsonArrayForSection);
for (int i = 0; i<jArray.length(); i++) {
JSONObject j = jArray.getJSONObject(i);
sectiondata = j.optString("Section_Name");
sectionlist = new ArrayList<String>();
sectionlist.add(sectiondata);
}
sectionSpinner
.setAdapter(new ArrayAdapter<String>(PutCredentials.this,
android.R.layout.simple_spinner_dropdown_item,
sectionlist));
} catch (JSONException e) {
e.printStackTrace();
}
}
private void getSubjectSpinner (){
try {
JSONArray jArraySubject = new JSONArray(jsonArrayForSubject);
for (int i = 0; i<jArraySubject.length(); i++) {
final JSONObject jsonObjectBatch = jArraySubject.getJSONObject(i);
subjectdata = jsonObjectBatch.optString("subject_name");
subjectlist = new ArrayList<String>();
subjectlist.add(subjectdata);
subjectSpinner
.setAdapter(new ArrayAdapter<String>(PutCredentials.this,
android.R.layout.simple_spinner_dropdown_item,
subjectlist));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
现在我的问题是,我的 Spinner 在 JSON 中仅显示 Btches、部分和主题,即 2016-2019、E 和 Hindi-1。 在此 Spinner 中显示“Section_Name”的最后一个值,即 Spinner 中的“E”。 如何在 Spinner 中获取键“Batch”“Section_Name”和“Subject_Name”的所有值。
【问题讨论】:
-
显然你应该使用带有 POJO 的适配器,而不是带有字符串的适配器
-
您一遍又一遍地将它存储在同一个变量中。你需要有一个
List<String>或一个String[] -
Google 对这些事情了解很多
标签: java android json spinner android-arrayadapter