我已經編寫了一個應用程序來獲取來自USGS的地震數據,我想爲想要使用不同mag等級檢查的用戶添加首選項更改。但是,如果首選項發生更改,onStart不會觸發。請建議我如何修改代碼SharedPreferences在更改時不啓動onStart
public class EarthquakeFragment extends Fragment{
public EarthquakeFragment(){
}
ListView listView;
private EarthquakeAdapter eAdapter;
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState)
{
View rootView = inflater.inflate(R.layout.earthquake_fragment, container, false);
eAdapter = new EarthquakeAdapter(getActivity(), new ArrayList<EarthquakeInfo>());
listView = (ListView) rootView.findViewById(R.id.list);
listView.setAdapter(eAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
EarthquakeInfo currentEarthquake = eAdapter.getItem(position);
String url = currentEarthquake.getUrl();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
setHasOptionsMenu(true);
// Start the AsyncTask to fetch the earthquake data
EarthquakeAsyncTask task = new EarthquakeAsyncTask();
task.execute();
return rootView;
}
/**
* {@link AsyncTask} to perform the network request on a background thread, and then
* update the UI with the list of earthquakes in the response.
*
* AsyncTask has three generic parameters: the input type, a type used for progress updates, and
* an output type. Our task will take a String URL, and return an Earthquake. We won't do
* progress updates, so the second generic is just Void.
*
* We'll only override two of the methods of AsyncTask: doInBackground() and onPostExecute().
* The doInBackground() method runs on a background thread, so it can run long-running code
* (like network activity), without interfering with the responsiveness of the app.
* Then onPostExecute() is passed the result of doInBackground() method, but runs on the
* UI thread, so it can use the produced data to update the UI.
*/
private class EarthquakeAsyncTask extends AsyncTask<String, Void, List<EarthquakeInfo>> {
/**
* This method runs on a background thread and performs the network request.
* We should not update the UI from a background thread, so we return a list of
* {@link EarthquakeInfo}s as the result.
*/
@Override
protected List<EarthquakeInfo> doInBackground(String... urls) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String maglevel = preferences.getString(getString(R.string.pref_mag_key), "");
final String BASE_URL= "http://earthquake.usgs.gov/fdsnws/event/1/query?";
final String FORMAT_PARAM = "format";
final String ORDER_PARAM = "orderby";
final String START_PARAM = "starttime";
final String END_PARAM = "endtime";
final String MINMAG_PARAM = "minmag";
String format = "geojson";
String order = "time";
String start = "2016-01-01";
String end = "2016-07-28";
String minmag = maglevel;
Uri buildUri = Uri.parse(BASE_URL).buildUpon()
.appendQueryParameter(FORMAT_PARAM, format)
.appendQueryParameter(ORDER_PARAM, order)
.appendQueryParameter(START_PARAM, start)
.appendQueryParameter(END_PARAM, end)
.appendQueryParameter(MINMAG_PARAM, minmag)
.build();
String url = buildUri.toString();
// Don't perform the request if there are no URLs, or the first URL is null.
if (url == null) {
return null;
}
List<EarthquakeInfo> result = QueryUtils.fetchEarthquakeData(url);
return result;
}
/**
* This method runs on the main UI thread after the background work has been
* completed. This method receives as input, the return value from the doInBackground()
* method. First we clear out the adapter, to get rid of earthquake data from a previous
* query to USGS. Then we update the adapter with the new list of earthquakes,
* which will trigger the ListView to re-populate its list items.
*/
@Override
protected void onPostExecute(List<EarthquakeInfo> data) {
// Clear the adapter of previous earthquake data
onStart();
eAdapter.clear();
// If there is a valid list of {@link Earthquake}s, then add them to the adapter's
// data set. This will trigger the ListView to update.
if (data != null && !data.isEmpty()) {
eAdapter.addAll(data);
}
}
}
}
非常感謝!!!!!
當共享首選項的值更改時,是否希望執行活動的onStart()事件? –
是的。這是我想要做的 –
不知道爲什麼有人會在沒有評論他們的問題是什麼問題的情況下倒下了。這應該停止。 – Talha