这是我的回答,我试图进入我的微调器,但它没有显示可以任何人告诉我我的代码中有什么错误..我的代码片段如下:我还添加了回复。谢谢你提前抱歉英语不好
{
"status":"success",
"country_list":
[
{
"country_id":237,
"name":"Zimbabwe"
},
{
"country_id":236,
"name":"Zambia"
}
]
}
class Country extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {
ArrayAdapter<String> adaptercountry ;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MyFragment.this.getActivity());
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
ServiceHandler sh = new ServiceHandler();
data = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(COUNTRY_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
country_list = jsonObj.getJSONArray(COUNTRY_LIST);
for (int i = 0; i < country_list.length(); i++) {
JSONObject c = country_list.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put(COUNTRY_ID, c.getString(COUNTRY_ID));
map.put(COUNTRY_NAME,c.getString(COUNTRY_NAME));
data.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return data;
}
protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
adaptercountry = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item);
spcountry.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View w) {
new AlertDialog.Builder(getActivity())
.setTitle("Select")
.setAdapter(adaptercountry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
spcountry.setText(adaptercountry.getItem(which).toString());
dialog.dismiss();
}
}).create().show();
}
});
}
答案 0 :(得分:1)
如果要在对话框中显示项目。为对话框设置单选项
new AlertDialog.Builder(MContext)
.setSingleChoiceItems(options, selectedIndex, itemClickListener)
.setPositiveButton(MContext.getString(R.string.dialog_done), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
})
.setNegativeButton(MContext.getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
})
.show();
其中选项应为国家/地区名称的CharSequence []
答案 1 :(得分:1)
目前您没有将国家/地区名称传递给适配器
protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
//prepare Array of country name
String[] arrConuntry=new String[data.size()];
for(int index=0;index<data.size();i++){
HashMap<String, String> map=data.get(index);
arrConuntry[index]=map.get(COUNTRY_NAME);
}
// pass arrConuntry array to ArrayAdapter<String> constroctor :
adaptercountry = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item,
arrConuntry);
///.....your code here
}
如果您想将data
作为数据源传递给ArrayAdapter
,那么您应该create custom adapter by extending ArrayAdapter