获取 Spinner 中的所有关键数据
get all Key Data in Spinner
我正在将 JSON 解析为微调器,但我没有在微调器中获取所有数据,
我正在 Spinner
中获取最后的关键数据
下面是 onPostExecute 方法。从我通过 Intent 将 Array 发送到 Next Class 的地方,即 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 Class
@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();
}
}
现在我的问题是,My Spinner 仅显示 JSON 中最后一位的 Btches、部分和主题,即 2016-2019、E 和 Hindi-1。
在此 Spinner 中显示 "Section_Name" 的最后一个值,即 Spinner 中的 "E"。
如何在微调器中获取键 "Batch" "Section_Name" 和 "Subject_Name" 的所有值。
这里你必须使用 Arraylist 而不是字符串,这样你就可以在 spinner.try this.and 中获取所有记录,然后这个 arraylist 绑定到你的 spinneradapter。
private ArrayList<String> aldata=new ArrayList<>();
JSONArray jArray = new JSONArray(jsonArrayForSection);
for (int i = 0; i<jArray.length(); i++)
{
JSONObject j = jArray.getJSONObject(i);
aldata.add(j.optString("Section_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);
拿下。
String data;
private void setSpinner (String jsonArrayFor,String jsonKey, Spinner spinner){
try {
JSONArray jArray = new JSONArray(jsonArrayFor);
arrayList = new ArrayList<String>();
for (int i = 0; i<jArray.length(); i++) {
JSONObject j = jArray.getJSONObject(i);
data = j.optString(jsonKey);
arrayList.add(data);
}
spinner
.setAdapter(new ArrayAdapter<String>(PutCredentials.this,
android.R.layout.simple_spinner_dropdown_item,
arrayList));
} catch (JSONException e) {
e.printStackTrace();
}
}
需要的时候调用它。
我正在将 JSON 解析为微调器,但我没有在微调器中获取所有数据, 我正在 Spinner
中获取最后的关键数据下面是 onPostExecute 方法。从我通过 Intent 将 Array 发送到 Next Class 的地方,即 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 Class
@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();
}
}
现在我的问题是,My Spinner 仅显示 JSON 中最后一位的 Btches、部分和主题,即 2016-2019、E 和 Hindi-1。 在此 Spinner 中显示 "Section_Name" 的最后一个值,即 Spinner 中的 "E"。 如何在微调器中获取键 "Batch" "Section_Name" 和 "Subject_Name" 的所有值。
这里你必须使用 Arraylist 而不是字符串,这样你就可以在 spinner.try this.and 中获取所有记录,然后这个 arraylist 绑定到你的 spinneradapter。
private ArrayList<String> aldata=new ArrayList<>();
JSONArray jArray = new JSONArray(jsonArrayForSection);
for (int i = 0; i<jArray.length(); i++)
{
JSONObject j = jArray.getJSONObject(i);
aldata.add(j.optString("Section_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);
拿下。
String data;
private void setSpinner (String jsonArrayFor,String jsonKey, Spinner spinner){
try {
JSONArray jArray = new JSONArray(jsonArrayFor);
arrayList = new ArrayList<String>();
for (int i = 0; i<jArray.length(); i++) {
JSONObject j = jArray.getJSONObject(i);
data = j.optString(jsonKey);
arrayList.add(data);
}
spinner
.setAdapter(new ArrayAdapter<String>(PutCredentials.this,
android.R.layout.simple_spinner_dropdown_item,
arrayList));
} catch (JSONException e) {
e.printStackTrace();
}
}
需要的时候调用它。