获取Spinner中的所有关键数据

时间:2017-01-18 12:32:51

标签: java android json spinner android-arrayadapter

我正在将JSON解析为spinner,但我没有在Spinner中获取所有数据,  我在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类

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

_____________________________ +++++ ___________________________________

这里我从Login 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,Sections和Subjects,即2016-2019,E和Hindi-1。 在这个Spinner中显示&#34; Section_Name&#34;的最后一个值。即&#34; E&#34;在Spinner中。 如何获得密钥&#34; Batch&#34;的所有值。 &#34; SECTION_NAME&#34;和&#34; Subject_Name&#34;在Spinner。

3 个答案:

答案 0 :(得分:1)

在arraylist中添加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);

答案 1 :(得分:0)

这里你必须使用Arraylist而不是字符串,这样你就可以在spinner.try中得到所有记录。然后在你的spinneradapter中使用这个arraylist绑定。

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

答案 2 :(得分:0)

取。

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

}

在需要时调用它。