0

Dear Friends, i want to get json array data's value from index and compare with my store id. For example:

i want to get instituteID from json array and compare with value "3" and if it is true then display stored subject value in textview if (instituteID == 3){tv.setText(subject);} instituteID and subject is also coming from json.tell me where i'm wrong

ArrayList<HashMap<String,String>> arraylist = new ArrayList<>();

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = (TextView)findViewById(R.id.subject1);
        tv2 = (TextView)findViewById(R.id.subject2);
        tv3 = (TextView)findViewById(R.id.subject3);
        tv4 = (TextView)findViewById(R.id.subject4);
        tv5 = (TextView)findViewById(R.id.subject5);
        tv6 = (TextView)findViewById(R.id.subject6);

//here calling json asynctask
new getTacherJson().execute();
        DisplayData();
}

public void DisplayData(){
        SharedPreferences preferences = getSharedPreferences("Email's response",MODE_PRIVATE);
        shared_pref_emal = preferences.getString("response",null);
        Log.e("Email's response",shared_pref_emal);

        SharedPreferences preferences1 = getSharedPreferences("Institute's ID",MODE_PRIVATE);
        shared_pref_ins_id = preferences1.getString("response id",null);
        Log.e("inst. id",shared_pref_ins_id);

        if (institute_ID == shared_pref_ins_id){
            tv1.setText(subject);
            tv2.setText(subject2);
            tv3.setText(subject3);
            tv4.setVisibility(View.GONE);
            tv5.setVisibility(View.GONE);
            tv6.setVisibility(View.GONE);
        }
        else {
            tv1.setText("Nothing");
        }
    }

//here is my Json Data
{"response":[{"id":"11","class":"One Class","section":"Morning","teacher":"[email protected]","subject":"Urdu","fromTime":"08:00","toTime":"08:30","instituteID":"3","created_at":"2019-09-12 04:39:31"},{"id":"12","class":"One Class","section":"Morning","teacher":"[email protected]","subject":"Pakistan Studies","fromTime":"08:30","toTime":"09:00","instituteID":"3","created_at":"2019-09-12 04:39:49"},{"id":"13","class":"BSSE","section":"Evening","teacher":"[email protected]","subject":"Introduction to CMP","fromTime":"15:00","toTime":"17:30","instituteID":"3","created_at":"2019-09-16 07:39:43"}]}


MyHttpHandler myhttp = new MyHttpHandler();
            String teacherURL = url+"/showTeacherCourses/"+get_email+"/"+shared_pref_ins_id;
            String teachDash = myhttp.MyServiceCall(teacherURL);
            Log.e(TAG,"Response From Teacher Courses"+teachDash);

        if (teachDash != null) {
            try {
                JSONObject jsonObject = new JSONObject(teachDash);
                JSONArray teachArray = jsonObject.getJSONArray("response");
                Log.e("Array length", String.valueOf(teachArray.length()));

                // looping through All Contacts
                for (int i = 0; i < teachArray.length(); i++) {
                    JSONObject c = teachArray.getJSONObject(i);

                    id1 = c.getString("id");
                    clasx = c.getString("class");
                    section = c.getString("section");
                    teacher_emaill = c.getString("teacher");
                    subject = c.getString("subject");
                    from_time = c.getString("fromTime");
                    to_time = c.getString("toTime");
                    institute_ID = c.getString("instituteID");
                    created_at = c.getString("created_at");


HashMap<String, String> tech_dta_ftch = new HashMap<>();

                        // adding each child node to HashMap key => value

                        tech_dta_ftch.put("id", id1);
                        tech_dta_ftch.put("class",clasx);
                        tech_dta_ftch.put("section",section);
                        tech_dta_ftch.put("teacher",teacher_emaill);
                        tech_dta_ftch.put("subject",subject);
                        tech_dta_ftch.put("fromTime",from_time);
                        tech_dta_ftch.put("toTime",to_time);
                        tech_dta_ftch.put("created_at", created_at);
                        tech_dta_ftch.put("instituteID", institute_ID);

                        // adding contact to contact list
                        arraylist.add(tech_dta_ftch);
2
  • post here your full json response Commented Sep 25, 2019 at 11:32
  • based on documents when using put() method: If the map previously contained a mapping for the key, the old value is replaced. more info Commented Sep 25, 2019 at 12:05

1 Answer 1

1

Try this :

for(int i = 0; i < teachArray.length(); i++){
    JSONObject c = teachArray.getJSONObject(i);
    HashMap<String, String> tech_dta_ftch = new HashMap<>();
    tech_dta_ftch.put("id", c.getString("id"));
    tech_dta_ftch.put("class",c.getString("class"));
    tech_dta_ftch.put("section",c.getString("section"));
    tech_dta_ftch.put("teacher",c.getString("teacher"));
    tech_dta_ftch.put("subject",c.getString("subject"));
    tech_dta_ftch.put("fromTime",c.getString("fromTime"));
    tech_dta_ftch.put("toTime",c.getString("toTime"));
    tech_dta_ftch.put("created_at", c.getString("created_at"));
    tech_dta_ftch.put("instituteID", c.getString("instituteID"));
    arraylist.add(tech_dta_ftch);   
}

And to compare institute ID of at a specific index of your array list currently I am selecting at first index, you could use it probably your way:

if (arraylist.get(0).get("instituteID").equalsIgnoreCase("ID_TO_CHECK_WITH")){
    //DO YOUR STUFF
}
Sign up to request clarification or add additional context in comments.

8 Comments

when i put this code and run then my activity closed due to this code if (arraylist.get(0).get("instituteID").equalsIgnoreCase("ID_TO_CHECK_WITH")){ //DO YOUR STUFF }
are you initializing your arraylist? Also can you share the code which is closing your application, if items are being added in the list, there should be no problem when retrieving those items
you need to put a break point when the item is being added in arraylist i.e. arraylist.add(tech_dta_ftch) and check the contents of your hashmap and do log the array size.
are you sure this exception is occurring once you get data from server? One cause may be that you are trying to get elements from an empty array list before the data is being added. You can put breakpoints in your code where you get response from server, and check the data which you receive and run debugger line by line just to make sure your hashmap is added into arraylist, developer.android.com/studio/debug You can have a look how to debug your app. Just make sure you are trying to get values from arraylist after server response.
You are trying to get data from list before it has that. Right way to do is to put displayData in onPostExecute so that your list is populated before use. Plz upvote and accept if it works for you(which it did).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.