Android Spinner使用mysql中的数据获取ID

时间:2017-09-16 09:31:26

标签: android mysql json listview android-spinner

我有一个带有片段类的Tab布局,里面有一个listview和一个微调器。列表视图中的所有数据都是从我的数据库中填充的。 如何通过在我的微调器中选择按ID获取的项目来更改列表视图中的数据?

以下是我的JSON文件示例

{
    "game_response": [{
        "EID": "1",
        "teamone": "RRR",
        "teamtwo": "TTT",
        "teamonepts": "69",
        "teamtwopts": "61",
        "e_name": "Basketball",
        "e_gender": "Male"
    }, {
        "EID": "2",
        "teamone": "AAA",
        "teamtwo": "BBB",
        "teamonepts": "61",
        "teamtwopts": "52",
        "e_name": "Basketball",
        "e_gender": "Female"
    }, {
        "EID": "3",
        "teamone": "CCC",
        "teamtwo": "DDD",
        "teamonepts": "56",
        "teamtwopts": "38",
        "e_name": "Football",
        "e_gender": "Male"
    }]
}

我的微调器中的每个数据的 EID是唯一ID 。当我选择" Basketball Female"时,如何才能在列表视图中显示来自EID 2的所有数据?来自我的微调器?

这是我的安卓代码

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.games, container, false);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        lv = (ListView) rootView.findViewById(R.id.listView);
        spinner1 = (Spinner) rootView.findViewById(R.id.sp1);
        getJSON();


        getSpinner();
        spinner_fn();

        return rootView;


    }

    private void getSpinner() {
        list1.clear();
        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();
        }

    }



    private void showJSON1() {
        JSONObject jsonObject;
        ArrayList<HashMap<String, String>> list9 = 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 EID = jo.getString(Config.TAG_EID);
                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_EID, EID);
                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);
                list9.add(match);
            }

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

        ListAdapter adapter = new SimpleAdapter(
                getActivity(), list, R.layout.gamesadapterlayout,
                new String[]{Config.TAG_EID, 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 GETJSON1() {
        class gETJSON1 extends AsyncTask<Void, Void, String> {


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

            }

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


            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                s = rh.sendGetRequest(Config.URL_VOLLEYBALL);
                return s;
            }
        }
        gETJSON1 gj = new gETJSON1();
        gj.execute();
    }



    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) {
                switch (position) {


                    case 0:
                        GETJSON1();
                        List<? extends Map<String, ?>> list = new ArrayList<>();
                        ListAdapter adapter = new SimpleAdapter(
                                getActivity(), list, R.layout.gamesadapterlayout,
                                new String[]{Config.TAG_EID, 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);
                 break;
                    case 1:
                       GETJSON1();
                        List<? extends Map<String, ?>> list = new ArrayList<>();
                        ListAdapter adapter = new SimpleAdapter(
                                getActivity(), list, R.layout.gamesadapterlayout,
                                new String[]{Config.TAG_EID, 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);
                        break;
                    case 2:

                        GETJSON1();
                        List<? extends Map<String, ?>> list = new ArrayList<>();
                        ListAdapter adapter = new SimpleAdapter(
                                getActivity(), list, R.layout.gamesadapterlayout,
                                new String[]{Config.TAG_EID, 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);
                        break;

0 个答案:

没有答案