我遵循本教程中有關解析JSON的內容。所有工作都應該按照我的需要進行編輯。我正在獲取有關「服務器」的信息,其中一個字段是「狀態」(UP或DOWN)。根據變量在單個列表視圖中更改文本顏色
編輯:忘了張貼教程鏈接http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
目前,它看起來像這樣:http://puu.sh/8Ky0B.jpg 以上是發生了什麼,然後應用程序被加載。點擊一次就是當單個項目被點擊時它開始一個新的活動並單獨顯示這些信息。
你可以看到第二個服務器的狀態是「向下」。基於此,我想將文字顏色更改爲紅色,並將其保持爲綠色。
我怎麼能做到這一點,當它經過,並將每個列表視圖?
這裏是一個(當前)基本上是一樣的教程的代碼:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serverList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name))
.getText().toString();
String ip = ((TextView) view.findViewById(R.id.ipAddress))
.getText().toString();
String status = ((TextView) view.findViewById(R.id.serverStatus))
.getText().toString();
// Starting single server activity
Intent in = new Intent(getApplicationContext(),
SingleContactActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_IP, ip);
in.putExtra(TAG_STATUS, status);
startActivity(in);
}
});
// Calling async task to get json
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONArray jArray = new JSONArray(jsonStr);
// looping through All Servers
for (int i = 0; i < jArray.length(); i++) {
JSONObject c = jArray.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String ip = c.getString(TAG_IP);
String status = c.getString(TAG_STATUS);
// tmp hashmap for single server
HashMap<String, String> contact = new HashMap<String, String>();
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_IP, ip);
contact.put(TAG_STATUS, status);
// adding server to server list
serverList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, serverList,
R.layout.list_item, new String[] { TAG_NAME, TAG_IP,
TAG_STATUS }, new int[] { R.id.name,
R.id.ipAddress, R.id.serverStatus });
setListAdapter(adapter);
}
}
如果您想自定義像這樣的列表項,請使用自定義適配器。 –