在我的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 :(得分:0)
根据您的问题,每当用户从您的微调器中选择项目时,您是否需要调用API?在这种情况下,将有两种可能性。
<强>第一强>
从一个请求中获取服务器中的所有数据,只有您的微调器的数据和您在微调框项目中显示的每个值的数据才能显示在ListView中。并将此数据安排到HashMap结构中。像这样:
HashMap<String,Object> allData = new HashMap<>();
在HashMap中,您的键将是Spinner项,值将是与该微调项对应的listview数据。现在,只要用户从微调器中选择任何项目,就可以根据所选项目获取值,并使用该数据填充列表视图,您需要在listview中使用新数据集再次设置适配器。
<强> SECOND 强>
获取要在微调器中显示的所有数据。现在,每当用户根据服务器中选择的项目选择任何项目获取数据并解析响应时,再次使用新数据集为列表视图设置适配器。
希望这会对你有所帮助。