如何使用 URL 处理多个微调器?
Posted
技术标签:
【中文标题】如何使用 URL 处理多个微调器?【英文标题】:How to work with multiple spinners using URLs? 【发布时间】:2018-09-16 03:02:45 【问题描述】:在我的应用程序中,我正在使用两个微调器,第一个微调器包含 Department 和 id 等数据,第二个微调器包含 deficiency 和 id,这里我正在使用一些 URL如下。
http://182.18.163.39/m/def.php?issue=1
此 URL 为我提供了第一个微调器值。从第一个微调器中选择一个值时,与该值相关的数据将进入第二个微调器。
这是我的第二个 URL,它提供了第二个微调器值。
"http://182.18.163.39/m/def.php?issue=2&dept="+dept_id
在这个 URL 中,我想从第一个 URL 传递 id,这是我的问题,我能够从第一个微调器获取所有值。当我从第一个微调器中选择一个值时,它不会在第二个微调器中为我提供相关值。 任何人都可以给我一个示例代码来解决这个问题。
代码如下:
//Department spinner
Spinner spinner;
String URL="http://182.18.163.39/train/m/def.php?issue=1";
ArrayList<String> Department;
String uid;
//Deficiency category spinner
Spinner spinner2;
String URL2 = "http://182.18.163.39/train/m/def.php?issue=2&dept="+dept_id;
ArrayList<String>Deficiency;
String defid;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.new_deficiency);
//spinner for department
Department = new ArrayList<>();
spinner=(Spinner)findViewById(R.id.select_department);
loadSpinnerData(URL);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
String department = spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();
Toast.makeText(getApplicationContext(),department,Toast.LENGTH_LONG).show();
@Override
public void onNothingSelected(AdapterView<?> adapterView)
// DO Nothing here
);
//spinner for deficiency
Deficiency = new ArrayList<>();
spinner2 = (Spinner)findViewById(R.id.deficiency_category);
loadSpinnerDeficiency(URL2);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
String deficiency = spinner2.getItemAtPosition(spinner2.getSelectedItemPosition()).toString();
Toast.makeText(getApplicationContext(),deficiency,Toast.LENGTH_LONG).show();
@Override
public void onNothingSelected(AdapterView<?> adapterView)
// DO Nothing here
);
private void loadSpinnerData(String url)
try
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet(url);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
JSONArray jsonarray = new JSONArray(responseString);
for (int i = 0; i < jsonarray.length(); i++)
JSONObject jsonobject = jsonarray.getJSONObject(i);
String department = jsonobject.getString("Department");
dept_id = jsonobject.getString("ID");
Department.add(department);
spinner.setAdapter(new ArrayAdapter<String>(NewDeficiency.this, android.R.layout.simple_spinner_dropdown_item, Department));
catch (JSONException e)
e.printStackTrace();
catch (ClientProtocolException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
private void loadSpinnerDeficiency(String url2)
try
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet("http://182.18.163.39/train/m/def.php?issue=2&dept="+dept_id);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
JSONArray jsonarray = new JSONArray(responseString);
for (int i = 0; i < jsonarray.length(); i++)
JSONObject jsonobject = jsonarray.getJSONObject(i);
String defcat = jsonobject.getString("Deficiency");
defid = jsonobject.getString("ID");
Deficiency.add(defcat);
spinner2.setAdapter(new ArrayAdapter<String>(NewDeficiency.this, android.R.layout.simple_spinner_dropdown_item, Deficiency));
catch (JSONException e)
e.printStackTrace();
catch (ClientProtocolException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
这是我对第一个 URL 的 json 响应:
[
"Department": "Select Department",
"ID": ""
,
"Department": "Engg North",
"ID": "1"
,
"Department": "DIESEL SHED",
"ID": "2"
,
"Department": "S & T",
"ID": "3"
,
"Department": "SAFETY",
"ID": "4"
,
"Department": "Srdenwest",
"ID": "5"
,
"Department": "enggwest",
"ID": "6"
,
"Department": "Engg central",
"ID": "7"
,
"Department": "electmaint",
"ID": "8"
,
"Department": "Operating",
"ID": "9"
,
"Department": "Security",
"ID": "10"
,
"Department": "Diesel Shed",
"ID": "11"
,
"Department": "Electric Loco shed",
"ID": "12"
,
"Department": "C & W",
"ID": "13"
,
"Department": "trso",
"ID": "14"
,
"Department": "trdohe",
"ID": "15"
,
"Department": "engg",
"ID": "16"
,
"Department": "commercial",
"ID": "17"
,
"Department": "engg South",
"ID": "18"
,
"Department": "Mechanical",
"ID": "19"
]
这是我对第二个 URL 的 json 响应:
[
"Deficiency": "Select Deficiency",
"ID": ""
,
"Deficiency": "Engg",
"ID": "1"
,
"Deficiency": "Track",
"ID": "25"
,
"Deficiency": "LCs",
"ID": "26"
,
"Deficiency": "Misc",
"ID": "43"
,
"Deficiency": "",
"ID": "NULL"
]
【问题讨论】:
请分享您正在使用的代码 请添加您的两个请求响应代码。 我更新了我的代码 我也更新了我的 json 响应 它没有给你一个正确的数据,因为你没有更新你的URL2
。尝试将您的loadSpinnerDeficiency(URL2);
并更改为loadSpinnerDeficiency(URL2 + departmentId);
在您的spinner.setOnItemSelectedListener onItemSelected
中。
【参考方案1】:
你应该创建一个自定义对象,比如
private HashMap<String,String>departmentMap=new HashMap<>();
private void loadSpinnerData(String url)
try
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet(url);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
JSONArray jsonarray = new JSONArray(responseString);
for (int i = 0; i < jsonarray.length(); i++)
JSONObject jsonobject = jsonarray.getJSONObject(i);
String department = jsonobject.getString("Department");
uid = jsonobject.getString("ID");
Department.add(department);
departmentMap.put(department,uid);
spinner.setAdapter(new ArrayAdapter<String>(NewDeficiency.this, android.R.layout.simple_spinner_dropdown_item, Department));
catch (JSONException e)
e.printStackTrace();
catch (ClientProtocolException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
并修改你的 firstSpinner onclick 像
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
String department=spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();
Toast.makeText(getApplicationContext(),department,Toast.LENGTH_LONG).show();
String id=departmentMap.get(department);
loadSpinnerDeficiency(URL2,id);
@Override
public void onNothingSelected(AdapterView<?> adapterView)
// DO Nothing here
);
现在您可以在 loadSpinnerDeficiency 方法中使用 id 来获得正确的响应
private void loadSpinnerDeficiency(String url2,String uid)
try
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet("http://182.18.163.39/train/m/def.php?issue=2&dept="+uid);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
JSONArray jsonarray = new JSONArray(responseString);
for (int i = 0; i < jsonarray.length(); i++)
JSONObject jsonobject = jsonarray.getJSONObject(i);
String defcat = jsonobject.getString("Deficiency");
defid = jsonobject.getString("ID");
Deficiency.add(defcat);
spinner2.setAdapter(new ArrayAdapter<String>(NewDeficiency.this, android.R.layout.simple_spinner_dropdown_item, Deficiency));
catch (JSONException e)
e.printStackTrace();
catch (ClientProtocolException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
【讨论】:
感谢这次重播,它工作正常,但是当我从第一个微调器中选择一个值时,它会在第二个微调器中提供相关值,这是一个小问题。但是在选择后我想改变第一个微调器的值,它带有微调器1的第一个和第二个值的值。如果我改变 spinner1 的值 spinner 的值也会改变怎么做 如果我更改了第一个微调器的项目,它仍然显示微调器 2 中第一个项目值的相关值如何解决它请帮帮我以上是关于如何使用 URL 处理多个微调器?的主要内容,如果未能解决你的问题,请参考以下文章
Android - 如何在一个活动中禁用多个微调器的特定项目