来自JSON我得到了服务器端respose。我也将数据设置为微调器。但是基于第一个微调器项目位置我必须得到第二个微调器的细节并且基于第二个微调器我必须得到第三个spinner.can的细节任何人PLZ阅读代码并发送给我回复 谢谢.....
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));
}
}
}
以下是我对所有微调旋转器的JSON响应:
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"}]}}
答案 0 :(得分:0)
您可以使用setOnItemSelectedListener()
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) {
}
});
答案 1 :(得分:0)
请搜索View.performClick()
。我认为它会解决你的问题。
在加载第一个spinner
数据时,请将位置保存在临时变量中,并为下一个按钮调用performclick()
,等等。