1

In my android app, I have a spinner and a listview. The spinner and listview data are populated by json. They are inside a fragment. My question is how do i change the content in my listview without changing the entire screen when an item in the spinner is selected? Thanks

Here is my spinner code

        try
        {
            URL url = new URL (Config.URL_SPIN);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            is = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            StringBuilder sb = new StringBuilder();
            while((line = bufferedReader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try
        {
            JSONArray JA = new JSONArray(result);
            JSONObject json;


            e_name = new String[JA.length()];
            e_gender = new String[JA.length()];

            for(int i = 0; i<JA.length(); i++)
            {
                json        =  JA.getJSONObject(i);
                e_gender[i] =  json.getString("e_gender");
                e_name[i]   =  json.getString("e_name");
            }
            list1.add("All");
            list1.add("NonSports");
            for(int i = 0; i<e_name.length; i++)
            {
                list1.add(e_name[i] + " "+e_gender[i]);
            }



        } catch (JSONException e) {
            e.printStackTrace();
        }
        spinner_fn();



        return rootView;


    }


private void spinner_fn() {
        ArrayAdapter<String> spinner = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, list1);
        spinner1.setAdapter(spinner);
        spinner1.setSelection(0);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {




        }

Here is my JSON code for the spinner

lv = (ListView) rootView.findViewById(R.id.listView2);
        spinner1 = (Spinner) rootView.findViewById(R.id.sp1);
        getJSON();
        try
        {
            URL url = new URL (Config.URL_SPIN);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            is = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            StringBuilder sb = new StringBuilder();
            while((line = bufferedReader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
    }

    try
    {
        JSONArray JA = new JSONArray(result);
        JSONObject json;


        e_name = new String[JA.length()];
        e_gender = new String[JA.length()];

        for(int i = 0; i<JA.length(); i++)
        {
            json        =  JA.getJSONObject(i);
            e_gender[i] =  json.getString("e_gender");
            e_name[i]   =  json.getString("e_name");
        }
        list1.add("All");
        list1.add("NonSports");
        for(int i = 0; i<e_name.length; i++)
        {
            list1.add(e_name[i] + " "+e_gender[i]);
        }



    } catch (JSONException e) {
        e.printStackTrace();
    }
    spinner_fn();



    return rootView;


}

And here is for the ListView

private void showResult() {
    JSONObject jsonObject;
    ArrayList<HashMap<String, String>> list = new ArrayList<>();
    try {
        jsonObject = new JSONObject(JSON_STRING);
        JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY2);

        for (int i = 0; i < result.length(); i++) {
            JSONObject jo = result.getJSONObject(i);
            String teamone = jo.getString(Config.TAG_teamone);
            String teamonepts = jo.getString(Config.TAG_teamonepts);
            String teamtwo = jo.getString(Config.TAG_teamtwo);
            String teamtwopts = jo.getString(Config.TAG_teamtwopts);
            String e_name = jo.getString(Config.TAG_e_name);
            String e_gender = jo.getString(Config.TAG_e_gender);
            String m_no = jo.getString(Config.TAG_m_no);
            HashMap<String, String> match = new HashMap<>();
            match.put(Config.TAG_teamone, teamone);
            match.put(Config.TAG_teamonepts, teamonepts);
            match.put(Config.TAG_teamtwo, teamtwo);
            match.put(Config.TAG_teamtwopts, teamtwopts);
            match.put(Config.TAG_e_name, e_name);
            match.put(Config.TAG_e_gender, e_gender);
            match.put(Config.TAG_m_no, m_no);
            list.add(match);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    ListAdapter adapter = new SimpleAdapter(
            getActivity(), list, R.layout.gamesadapterlayout,
            new String[]{Config.TAG_teamone, Config.TAG_teamonepts, Config.TAG_teamtwo, Config.TAG_teamtwopts, Config.TAG_e_name, Config.TAG_e_gender, Config.TAG_m_no},
            new int[]{R.id.team1, R.id.score1, R.id.team2, R.id.score2, R.id.Type, R.id.s_gender});
    lv.setAdapter(adapter);
}

private void getJSON() {
    class GetJSON extends AsyncTask<Void, Void, String> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            JSON_STRING = s;
            showResult();
        }


        @Override
        protected String doInBackground(Void... params) {
            RequestHandler rh = new RequestHandler();
            s = rh.sendGetRequest(Config.URL_GAMES);
            return s;
        }
    }
    GetJSON gj = new GetJSON();
    gj.execute();
}
orange
  • 717
  • 2
  • 6
  • 14
  • Where is the `JSON` which you want to set in `listview` when you select the spinner item? Other thing is what is the issue when you implement code for `listview`? – Farmer Aug 08 '17 at 14:40
  • added the json sir. What i have in my listview is every data in my database. I want to use the spinner to filter the data that is shown in my listview – orange Aug 08 '17 at 15:41
  • You just call `getJSON();` method in your spinner `OnItemSelectedListener` and pass that data only in you `SimpleAdatapter` instead of whole `list` – Farmer Aug 09 '17 at 08:23
  • how do i do it sir? – orange Aug 24 '17 at 03:00

0 Answers0