How to show second spinner data based on first spinner. Json is coming from url?

Course Queries Syllabus Queries 3 years ago

2.5K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago


Till now I am getting JSON and putting them in 3 different spinners. But the problem is that how to make 2nd spinner data based on the 1st spinner and 3rd spinner data based on the 2nd spinner.

       This is my code for JSON.

            try {
            // Locate the NodeList name
            jsonarray = jsonobject.getJSONArray("results");
            for (int i = 0; i < jsonarray.length(); i++) {
                jsonobject = jsonarray.getJSONObject(i);

                Results worldpop = new Results();

                worldpop.setSyllabus(jsonobject.optString("syllabus"));
                worldpop.setGrade(jsonobject.optString("grade"));
                worldpop.setSubject(jsonobject.optString("subject"));
                //worldpop.setFlag(jsonobject.optString("flag"));
                world.add(worldpop);


                worldlist.add(jsonobject.optString("syllabus"));
                worldlist2.add(jsonobject.optString("grade"));
                worldlist3.add(jsonobject.optString("subject"));

            }
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {
        // Locate the spinner in activity_main.xml

        final Spinner mySpinner = (Spinner) findViewById(R.id.syllabus);
        Spinner mySpinner2 = (Spinner) findViewById(R.id.grade);
        Spinner mySpinner3 = (Spinner) findViewById(R.id.subject);

        // Spinner adapter


        LinkedHashSet<String> listToSet = new LinkedHashSet<String>(worldlist);

        //Creating Arraylist without duplicate values
        final List<String> worldlistnew = new ArrayList<String>(listToSet);




     mySpinner
                .setAdapter(new ArrayAdapter<
                                                
0 views
0 shares

profilepic.png
manpreet 3 years ago


Here is the answer. In this example depending on first spinner second spinner values gets changes. and it will also change the text in the actitivty, which ever value you are selecting in second spinner. If this helps you please vote.

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Html;
import android.view.Menu;
 import android.view.View.OnClickListener;
 import java.util.ArrayList;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.Spinner;
 import android.widget.TextView;

 public s://forum.tuteehub.com/tag/class">class Courses extends Activity {




Spinner s1,s2;
TextView seats_t,t2,hod_det;
ImageView im1;
String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.courses);



    s1 = (Spinner) findViewById(R.id.grad);
    s2 = (Spinner) findViewById(R.id.streams);
    seats_t=(TextView)findViewById(R.id.about1);
    t2=(TextView)findViewById(R.id.hodname1);
    hod_det=(TextView)findViewById(R.id.hoddetails);
    im1=(ImageView)findViewById(R.id.hodimage);
    t2.
                                                    
0 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum