gpt4 book ai didi

java - 获取 Spinner 中的所有关键数据

转载 作者:行者123 更新时间:2023-12-01 09:00:24 27 4
gpt4 key购买 nike

我正在将 JSON 解析到 spinner 中,但我没有在 Spinner 中获取所有数据, 我正在 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();
}`

_________________________+++++___________________________________

这里我从登录 Activity 获取数据。

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();
}
}

现在我的问题是,我的微调器仅显示 JSON 中最后位置的 Btches、部分和主题,即 2016-2019、E 和 Hindi-1。在此微调器中显示“Section_Name”的最后一个值,即微调器中的“E”。如何获取 Spinner 中键“Batch”、“Section_Name”和“Subject_Name”的所有值。

最佳答案

在数组列表中添加 jsonobject 字符串:

ArrayList<String> spinnerArray = new ArrayList<String>()   

public ArrayList<String> getList() {

JSONArray jArray = new JSONArray(jsonArrayForSection);
for (int i = 0; i<jArray.length(); i++) {
JSONObject j = jArray.getJSONObject(i);
sectiondata = j.optString("Section_Name");
spinnerArray.add(sectiondata);
}
return spinnerArray;
}

然后

 Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getList()); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);

关于java - 获取 Spinner 中的所有关键数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41719544/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com