Skip to content Skip to sidebar Skip to footer

Displaying Corresponding Json Data In Textview After Selecting Item In Spinner

my spinner contains the unique standard value from json(like: my spinner contains only 7,8,6 (which i want,)instead of displaying all standard repitative data), if spinner item is

Solution 1:

So you want to get all students details from selected standards. Here is the code that will solve your problem.

Create value boject class with getter's and setter's

publicclassStudentVOimplementsSerializable{

    privateString name;
    privateString surname;
    privateString age;
    privateString div;
    privateString standard;

    publicStringgetName() {
        return name;
    }

    publicvoidsetName(String name) {
        this.name = name;
    }

    publicStringgetSurname() {
        return surname;
    }

    publicvoidsetSurname(String surname) {
        this.surname = surname;
    }

    publicStringgetAge() {
        return age;
    }

    publicvoidsetAge(String age) {
        this.age = age;
    }

    publicStringgetDiv() {
        return div;
    }

    publicvoidsetDiv(String div) {
        this.div = div;
    }

    publicStringgetStandard() {
        return standard;
    }

    publicvoidsetStandard(String standard) {
        this.standard = standard;
    }
}

Now in your main class where you are selecting standard from spinner replace your code with:

//Add thispublicclassMainActivityextendsAppCompatActivity {

ArrayList<String> AllStandards = newArrayList<>();
private ArrayList<StudentVO> studentVOList = newArrayList<>();
ArrayAdapter<String> adapter;
JSONArray jsonArray;
Spinner spinner;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final List<String> items = getCountries("data.json");

    spinner = (Spinner) findViewById(R.id.spinnerStandard);
    adapter = newArrayAdapter<String>(this, R.layout.second_layout, R.id.txtStandard, items);

    spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {

        publicvoidonItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            try {

                Stringstandard= AllStandards.get(i);

                if (studentVOList.size() > 0)
                    studentVOList.clear();
                for (intj=0; j < jsonArray.length(); j++) {
                    JSONObjectjsonObject= jsonArray.getJSONObject(j);
                    Stringstand= jsonObject.getString("standard");
                    if (stand.equalsIgnoreCase(standard)) {
                        StudentVOstudentVO=newStudentVO();
                        studentVO.setAge(jsonObject.getString("age"));
                        studentVO.setName(jsonObject.getString("name"));
                        studentVO.setDiv(jsonObject.getString("div"));
                        studentVO.setStandard(stand);
                        studentVOList.add(studentVO);
                    }
                }

                Log.d("TAG", "List With All Students in selected standard: "+studentVOList.size());

                Intentintent=newIntent(getApplicationContext(), StudentsInfo.class);
                Bundleb=newBundle();
                b.putSerializable("list",studentVOList);

                intent.putExtra("bundle",b);

                startActivity(intent);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        publicvoidonNothingSelected(AdapterView<?> arg0) {
        }
    });
    spinner.setAdapter(adapter);

}//onCreate Methodprivate List<String> getCountries(String fileName) {
    jsonArray = null;


    //ArrayList<String> cList = new ArrayList<String>();try {
        InputStreamis= getResources().getAssets().open(fileName);
        intsize= is.available();
        byte[] data = newbyte[size];
        is.read(data);
        is.close();
        Stringjson=newString(data, "UTF-8");

        AllStandards.clear();
        try {
            jsonArray = newJSONArray(json);

            for (inti=0; i < jsonArray.length(); i++) {
                JSONObjectjsonObject= jsonArray.getJSONObject(i);
                Stringstand= jsonObject.getString("standard");
                if (!AllStandards.contains(stand)) {
                    AllStandards.add(stand);
                }
            }
        }
        catch (JSONException je) {
            je.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }catch (IOException e) {
        e.printStackTrace();
    }
    return AllStandards;
}
}

After this you can get this list in StudentsInfo class like:

Bundle b = getIntent().getBundleExtra("bundle");
ArrayList<StudentVO> studentVOList = (ArrayList<StudentVO>) b.getSerializable("list");

Log.d("TAG", "List Size; "+studentVOList.size());

And you don't need to get list of standards from json array. You can prepare static list of students with standards 1,2,3.... so on and pass it to your array adapter.

See my debug result:enter image description here

I just check your code Remove

intent.putExtra("name", jsonArray.optJSONObject(i).getString("name"));
                    intent.putExtra("surname", jsonArray.optJSONObject(i).getString("surname"));
                    intent.putExtra("age", jsonArray.optJSONObject(i).getString("age"));
                    intent.putExtra("div", jsonArray.optJSONObject(i).getString("div"));

code from your MainActivity and in onCreate() method of StudentInfo activity class You can get list of students like:

Bundle b = getIntent().getBundleExtra("bundle");
    ArrayList<StudentVO> studentVOList = (ArrayList<StudentVO>) b.getSerializable("list");

Solution 2:

its because in your spinner itemSelected listener you are getting jsonArray.optJSONObject(i) here i is position of spinner item and not the standard. You will have get the value of this standard like this from spinner

spinner.getItemAtPosition(i);

and match it with the standard of all elements in JSONArray and add it in a list. then pass this list in your intent.

intent.putStringArrayListExtra("test", (ArrayList<String>) test);

Post a Comment for "Displaying Corresponding Json Data In Textview After Selecting Item In Spinner"