How to put Json data in spinners from URL?

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

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


I am getting my json in text box I tried to put those data in spinner but unable to do so. Below is my main activity class and I am using Volley.

    public class Mainactivity  extends Activity {
        private String urlJsonArry = "https://www.abc.json";
        private static String TAG = MainActivity.class.getSimpleName();
        private Button btnMakeArrayRequest;

        // Progress dialog
        private ProgressDialog pDialog;

        private TextView txtResponse,txtResponse2,txtResponse3,txtResponse4;

        // temporary string to show the parsed response
        private String jsonResponse, jsonResponse2, jsonResponse3, jsonResponse4;
        Spinner spinner;
        ArrayAdapter<String> adapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.spinners);

            spinner= (Spinner) findViewById(R.id.spinner);




            btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest);
            txtResponse = (TextView) findViewById(R.id.txtResponse);
            txtResponse2 = (TextView) findViewById(R.id.txtResponse2);
            txtResponse3 = (TextView) findViewById(R.id.txtResponse3);
            //  txtResponse4 = (TextView) findViewById(R.id.txtResponse4);


            pDialog = new ProgressDialog(this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);

            btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // making json array request
                    makeJsonArrayRequest();
                }
            });

        }



        private void makeJsonArrayRequest () {

        showpDialog();
        JsonObjectRequest req = new JsonObjectRequest(urlJsonArry,

                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

You need to use the Activity context this is not the activity context try like this

adapter= new ArrayAdapter<String>(Mainactivity.this, android.R.layout.simple_spinner_item, array);

Edit :

Change your array format. For example in your spinner you are going to show the subject so do the following

String[] mySubject = new String[array.length()];
for (int i = 0; i < array.length(); i++) {

                                JSONObject person = (JSONObject) array
                                        .get(i);

                                System.out.println(person.toString());

                                String syllabus = person.getString("grade");
                                String grade = person.getString("grade");
                                String subject = person.getString("subject");
                                jsonResponse += "Board: " + syllabus + "\n\n";
                                jsonResponse2 += "Class: " + grade + "\n\n";
                                jsonResponse3 += "Subject: " + subject + "\n\n";
                                // jsonResponse4 += "ID: " + id + "\n\n";

                                mySubject[i] = subject;

}


 adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mySubject);
                                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                                spinner.setAdapter(adapter);

Note : Also you need to move your set adapter outside of for loop.


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.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community