我在屏幕上刷新数据有问题,app通过JSON获取数据一切正常,但现在我需要在菜单中执行刷新按钮。我根据一个例子构建了我的应用程序。据我所知,使用SimpleAdapter并且需要编写自定义适配器是个好主意。你能帮帮我吗?
的ServiceHandler
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
MainActivity
public class MainActivity extends ListActivity {
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "******";
// JSON Node names
private static final String TAG_ZLECENIA = "zlecenia";
private static final String TAG_ID = "id";
private static final String TAG_CO = "co";
private static final String TAG_KYDA = "kyda";
private static final String TAG_ILE = "ile";
private static final String TAG_LIM = "lim_czas";
// zlecenia JSONArray
JSONArray zlecenia = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> ZleceniaList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ZleceniaList = 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 co = ((TextView) view.findViewById(R.id.co))
.getText().toString();
String kyda = ((TextView) view.findViewById(R.id.kyda))
.getText().toString();
}
});
// Calling async task to get json
new GetContacts().execute();
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* 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 {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
zlecenia = jsonObj.getJSONArray(TAG_ZLECENIA);
// looping through All Contacts
for (int i = 0; i < zlecenia.length(); i++) {
JSONObject c = zlecenia.getJSONObject(i);
String id = c.getString(TAG_ID);
String co = c.getString(TAG_CO);
String kyda = c.getString(TAG_KYDA);
String ile = c.getString(TAG_ILE);
String lim = c.getString(TAG_LIM);
// tmp hashmap for single contact
HashMap<String, String> zlecenia = new HashMap<String, String>();
// adding each child node to HashMap key => value
zlecenia.put(TAG_ID, id);
zlecenia.put(TAG_CO, co);
zlecenia.put(TAG_KYDA, kyda);
zlecenia.put(TAG_ILE, ile);
zlecenia.put(TAG_LIM, lim);
// adding contact to contact list
ZleceniaList.add(zlecenia);
}
} 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, ZleceniaList, R.layout.list_item,
new String[] { TAG_KYDA, TAG_CO, TAG_ILE, TAG_LIM,},
new int[] { R.id.kyda, R.id.co, R.id.ile, R.id.lim });
setListAdapter(adapter);
}
}
}
休息/菜单/ main.xml中
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item
android:id="@+id/action_refresh"
android:showAsAction="ifRoom"
android:title="Refresh"
android:icon="@drawable/ic_refresh_white_36dp"
/>
</menu>
答案 0 :(得分:0)
您必须覆盖 onOptionsItemSelected()方法并处理刷新菜单项单击操作。