如何选择我的微调项目中的项目时更改我的列表视图的内容?

问题描述:

在我的Android应用程序中,我有一个微调和一个列表视图。微调和列表视图数据由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) { 




     } 

下面是微调

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; 


} 

我的JSON代码这里是为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

当您在微调器中选择一个项目时,是否需要调用另一个Json请求? –

+0

只要在微调器中设置了一些值,并设置适配器'adapter = new SimpleAdapter(无论,new,values,you,need,to,pass),为列表视图创建一个新对象;'然后设置适配器' lv.setAdapter(adapter);' –

+0

@SakuraFukuyoshi是的,我试图调用另一个json请求,只要用户从微调框中选择一个值 – orange

从你的问题是不清楚每次用户从您的微调器中选择项目时是否需要调用API?在这种情况下,会有两种可能性。

FIRST

获取从一个请求服务器中的所有数据仅是您微调和数据的微调项目的每个值在ListView中显示的数据。并将这些数据安排到HashMap结构中。事情是这样的:

HashMap<String,Object> allData = new HashMap<>(); 

在HashMap的钥匙将微调项和值将对应于微调项列表视图的数据。现在,每当用户从您的微调器中选择任何项目时,根据所选项目获取值并使用该数据填充您的列表视图,您需要在新列表视图中再次使用新数据集设置适配器。

SECOND

抓取所有数据在旋转显示。现在,每当用户根据服务器上选择的项目选择任何项目获取数据并解析响应,并再次使用新数据集为您的列表视图设置适配器。

希望这可以帮助你。