我有个问题。我没有将 JSONArray 设置为 Spinner。我的 JSON 外观["category1","category2","category3"]
如何让这个 JSONArray 变成微调器?不知道我的代码好不好
public class Main extends Activity {
String urlCat = "http://tvapp.pcrevue.sk/categories.json";
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
JSONArray jsonArray = getJSONArrayFromUrl(urlCat);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
ArrayAdapter<String> spinnerMenu = new ArrayAdapter<String>(actionBar.getThemedContext(), android.R.layout.simple_list_item_1, jsonArray);
actionBar.setListNavigationCallbacks(spinnerMenu,
new ActionBar.OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
FragmentTransaction tx = getFragmentManager().beginTransaction();
switch (itemPosition) {
case 0:
tx.replace(android.R.id.content, new Tab1Fragment());
break;
case 1:
tx.replace(android.R.id.content, new Tab2Fragment());
break;
default:
break;
}
tx.commit();
return false;
}
});
}
public JSONArray getJSONArrayFromUrl(String url) {
InputStream is = null;
JSONArray jObj = null;
String json = "";
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
//json += line;
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONArray(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
我需要你的帮助。谢谢