当选择了微调器中的项目时,如何在片段内更改listview的内容?

时间:2017-08-08 14:30:01

标签: android android-fragments spinner

在我的Android应用程序中,我有一个微调器和一个列表视图。微调器和listview数据由json填充。它们在片段内。我的问题是,当选择微调器中的项目时,如何在不更改整个屏幕的情况下更改列表视图中的内容?感谢

这是我的微调器代码

        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) {




        }

这是我的微调器的JSON代码

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;


}

这里是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();
}

0 个答案:

没有答案