Android 基于第一个微调器如何获取第二个微调器和第三个微调器数据
从JSON我得到了服务器端响应。我还将数据设置为微调器。但根据第一个微调器项目的位置,我必须获得第二个微调器的详细信息,根据第二个微调器,我必须获得第三个微调器的详细信息。有人可以读取代码并向我发送响应吗 谢谢你Android 基于第一个微调器如何获取第二个微调器和第三个微调器数据,android,Android,从JSON我得到了服务器端响应。我还将数据设置为微调器。但根据第一个微调器项目的位置,我必须获得第二个微调器的详细信息,根据第二个微调器,我必须获得第三个微调器的详细信息。有人可以读取代码并向我发送响应吗 谢谢你 public class Send extends Fragment { Spinner sp1, sp2, sp3; Button b1, b2; Bitmap bmp; String image; int category_id; ImageView iv
public class Send extends Fragment {
Spinner sp1, sp2, sp3;
Button b1, b2;
Bitmap bmp;
String image;
int category_id;
ImageView iview;
Intent i = new Intent();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.send, container, false);
sp1 = (Spinner) v.findViewById(R.id.categories);
sp2 = (Spinner) v.findViewById(R.id.selectCity);
sp3 = (Spinner) v.findViewById(R.id.selectArea);
b1 = (Button) v.findViewById(R.id.search);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyTask task = new MyTask();
task.execute();
Intent i = new Intent();
i.setComponent(new ComponentName(getActivity(), Image.class));
i.putExtra("image", image);
startActivity(i);
}
});
b2 = (Button) v.findViewById(R.id.clear);
new DownloadJSON().execute();
new City().execute();
new Area().execute();
/* new MyTask().execute(); */
return v;
}
class MyTask extends AsyncTask<Void, Void, Void> {
String msg = "";
URL url;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
/*
* URL url = new URL(
* "http://creersoft.com/webservices/StoreService.php?categories="
* +
* sp1.getSelectedItem().toString()+"&city="+sp2.getSelectedItem
* ().toString()+"&area="+sp3.getSelectedItem().toString());
*/
url = new URL(
"http://creersoft.com/webservices/StoreService.php?categories="
+ sp1.getSelectedItem().toString() + "&city="
+ sp2.getSelectedItem().toString() + "&area="
+ sp3.getSelectedItem().toString());
InputStream isr = url.openStream();
int i = isr.read();
while (i != -1) {
msg = msg + (char) i;
i = isr.read();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
try {
JSONObject mainObject = new JSONObject(msg);
JSONArray jsonArray = mainObject.getJSONArray("Result");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject subObject = jsonArray.getJSONObject(i);
image = subObject.getString("image");
Toast.makeText(getActivity(), image, Toast.LENGTH_LONG)
.show();
int id = subObject.getInt("id");
Toast.makeText(getActivity(), id, Toast.LENGTH_LONG).show();
String storename = subObject.getString("storename");
Toast.makeText(getActivity(), storename, Toast.LENGTH_LONG)
.show();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
// Download JSON file AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
JSONObject jsonobject;
JSONArray jsonarray;
ArrayList<String> worldlist;
ArrayList<WorldPopulation> world;
/*
* ArrayList<String> listnew; ArrayList<String> sp;
*/
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Fetching food categories..");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Locate the WorldPopulation Class
world = new ArrayList<WorldPopulation>();
worldlist = new ArrayList<String>();
jsonobject = JSONfunctions
.getJSONfromURL("http://creersoft.com/webservices/getcategory.php");
try {
JSONObject maJsonObject = jsonobject.getJSONObject("Response");
JSONArray jsonArray = maJsonObject.getJSONArray("Result");
for (int i = 0; i < jsonArray.length(); i++) {
jsonobject = jsonArray.getJSONObject(i);
category_id=jsonobject.getInt("id");
Toast.makeText(getActivity(), category_id, Toast.LENGTH_LONG).show();
WorldPopulation worldpop = new WorldPopulation();
worldpop.setId(jsonobject.optInt("id"));
worldpop.setName(jsonobject.optString("name"));
world.add(worldpop);
// Populate spinner with country names
worldlist.add(jsonobject.optString("name"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the spinner in activity_main.xml
if (pDialog.isShowing())
pDialog.dismiss();
sp1.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item, worldlist));
}
}
class City extends AsyncTask<Void, Void, Void> {
JSONObject jsonobject;
JSONArray jsonarray;
ProgressDialog pDialog;
ArrayList<String> worldlist;
ArrayList<WorldPopulation> world;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Fetching Cities information..");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
world = new ArrayList<WorldPopulation>();
worldlist = new ArrayList<String>();
jsonobject = JSONfunctions
.getJSONfromURL("http://creersoft.com/webservices/getcity.php?category_id="+category_id);
try {
JSONObject maJsonObject = jsonobject.getJSONObject("Response");
JSONArray jsonArray = maJsonObject.getJSONArray("Result");
for (int i = 0; i < jsonArray.length(); i++) {
jsonobject = jsonArray.getJSONObject(i);
WorldPopulation worldpop = new WorldPopulation();
worldpop.setName(jsonobject.optString("city"));
world.add(worldpop);
worldlist.add(jsonobject.optString("city"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
sp2.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item, worldlist));
}
}
class Area extends AsyncTask<Void, Void, Void> {
JSONObject jsonobject;
JSONArray jsonarray;
ProgressDialog pDialog;
ArrayList<String> worldlist;
ArrayList<WorldPopulation> world;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Fetching Areas Information..");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
world = new ArrayList<WorldPopulation>();
worldlist = new ArrayList<String>();
jsonobject = JSONfunctions
.getJSONfromURL("http://creersoft.com/webservices/getarea.php?category_id="+category_id+"&city_id=Lagos");
try {
JSONObject maJsonObject = jsonobject.getJSONObject("Response");
JSONArray jsonArray = maJsonObject.getJSONArray("Result");
for (int i = 0; i < jsonArray.length(); i++) {
jsonobject = jsonArray.getJSONObject(i);
WorldPopulation worldpop = new WorldPopulation();
worldpop.setName(jsonobject.optString("area"));
world.add(worldpop);
worldlist.add(jsonobject.optString("area"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
sp3.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item, worldlist));
}
}
}
您可以使用setOnItemSelectedListener()
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView arg0、视图arg1、内部arg2、长arg3){
String items=spinner.getSelectedItem().toString();
Log.i(“选定项:”,项);
}
@凌驾
未选择公共无效(AdapterView arg0){
}
});
请搜索有关查看.性能单击()
。我想这会解决你的问题
加载第一个
微调器
数据时,将位置保存在临时变量中,并调用performclick()
以获取下一个按钮,依此类推。使用setOnItemSelectedListener。第一个微调器的第一个载荷值。然后,在第二个喷丝器的onitemSelected中,您将根据pos调用wedservice加载第二个喷丝器的值获得第一个喷丝器的pos selected,同样,在第二个喷丝器的onitemSelected中,您可以调用webservice获取第三个喷丝器的值感谢回复用户2564055。我尝试了SetonimSelected,但没有得到输出。除了这个,还有人能给我建议吗。user2564055你能告诉我你用实际的方式告诉我的吗?DevManu我也尝试了这个,但仍然没有得到输出。你能给我发一个关于这个的简短代码吗。首先告诉我你所有的任务都是独立的,您正在调用oncreate()方法中的所有任务。但我认为每个任务都依赖于其他任务,所以在调用第一个任务的onPostExecute()时调用第二个任务。如果我在类别的onPostExecute中提供异步任务,我会得到空指针异常。查看您的代码,您提供了错误的类别id和城市id,它不是动态的,我认为城市id和类别id将在微调器选择之后出现。jsonobject=JSONfunctions.getJSONfromURL(“);
Spinner1:{"Response":{"Success":"1","Result":[{"id":"1","name":"Groceries & Convenience"},{"id":"2","name":"Pharmacy "},{"id":"3","name":"Fashion & Accessories"},{"id":"4","name":"Electronics"},{"id":"5","name":"Beauty & Wellness"},{"id":"6","name":"Food"},{"id":"7","name":"Books & Games"},{"id":"8","name":"For Kids"},{"id":"9","name":"Home & Living"},{"id":"10","name":"Travel & Hotels"}]}}spinner2:{"Response":{"Success":"1","Result":[{"city":"Kano"},{"city":"Lagos"}]}}spinner3:-{"Response":{"Success":"1","Result":[{"area":"Lekki"}]}}
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String items = spinner.getSelectedItem().toString();
Log.i("Selected item : ", items);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});