![](/img/trans.png)
[英]How to parse multiple Json data in android that coming from the server
[英]Loader, Json and reload the data coming from the server
我正在開發一個簡單的小型應用程序(作為練習),該應用程序旨在從JSON文件中收集一些數據並顯示在活動中。 相同的活動還具有一個微調器,當用戶選擇一個元素時,應通過將參數修改到服務器並從JSON文件獲取不同信息的參數來“重新加載” Loader。
public class ChooseMatchActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Match>> {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_match);
Intent intent = getIntent();
mCurrentPetUri = intent.getData();
ArrayList<String> days = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd-MMM-yyyy");
for (int i = 0; i < 7; i++) {
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.DATE, i);
String day = sdf.format(calendar.getTime());
days.add(day);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, days);
final Spinner spinDays = (Spinner)findViewById(R.id.spinner_days);
spinDays.setAdapter(adapter);
spinDays.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
setMatchesOfTheDay(spinDays.getSelectedItem().toString().toLowerCase());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
ListView matchListView = (ListView) findViewById(R.id.list);
mAdapter = new MatchAdapter(this, new ArrayList<Match>());
matchListView.setAdapter(mAdapter);
mEmptyStateTextView = (TextView) findViewById(R.id.empty_view);
matchListView.setEmptyView(mEmptyStateTextView);
mStateProgressBar = (ProgressBar) findViewById(R.id.loading_spinner);
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
// Get details on the currently active default data network
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
// If there is a network connection, fetch data
if (networkInfo != null && networkInfo.isConnected()) {
// Get a reference to the LoaderManager, in order to interact with loaders.
LoaderManager loaderManager = getLoaderManager();
// Initialize the loader. Pass in the int ID constant defined above and pass in null for
// the bundle. Pass in this activity for the LoaderCallbacks parameter (which is valid
// because this activity implements the LoaderCallbacks interface).
loaderManager.initLoader(MATCH_LOADER_ID, null, this);
} else {
// Otherwise, display error
// First, hide loading indicator so error message will be visible
mStateProgressBar.setVisibility(View.GONE);
mEmptyStateTextView.setText(R.string.no_internet_connection);
}
}
以下是我用來處理加載程序並攔截微調器上選擇的值的4種方法
public void setMatchesOfTheDay(String day) {
Toast.makeText(this, "You choose the day: " + day,
Toast.LENGTH_SHORT).show();
Uri baseUri = Uri.parse(USGS_REQUEST_URL);
Uri.Builder uriBuilder = baseUri.buildUpon();
uriBuilder.appendQueryParameter("format", "geojson");
uriBuilder.appendQueryParameter("limit", "30");
new MatchLoader(this, uriBuilder.toString());
}
@Override
public Loader<List<Match>> onCreateLoader(int i, Bundle bundle ) {
// Create a new loader for the given URL
Uri baseUri = Uri.parse(USGS_REQUEST_URL);
Uri.Builder uriBuilder = baseUri.buildUpon();
uriBuilder.appendQueryParameter("format", "geojson");
uriBuilder.appendQueryParameter("limit", "10");
return new MatchLoader(this, uriBuilder.toString());
}
@Override
public void onLoadFinished(Loader<List<Match>> loader, List<Match> matches) {
// Set empty state text to display "No earthquakes found."
mEmptyStateTextView.setText(R.string.no_matches);
mStateProgressBar.setVisibility(View.GONE);
// Clear the adapter of previous earthquake data
mAdapter.clear();
// If there is a valid list of {@link Match}s, then add them to the adapter's
// data set. This will trigger the ListView to update.
if (matches != null && !matches.isEmpty()) {
mAdapter.addAll(matches);
}
}
@Override
public void onLoaderReset(Loader<List<Match>> loader) {
// Loader reset, so we can clear out our existing data.
mAdapter.clear();
}
第一次訪問該活動時,一切運行正常,但是一旦從微調器中選擇一個元素,我就可以看到Toast消息,但列表視圖中沒有任何變化。 我嘗試了幾種選擇,但在使用Loader時確實感到困惑,希望有人可以澄清一些概念
您的setMatchesOfTheDay
方法正在調用new MatchLoader(this, uriBuilder.toString());
,但沒有執行任何操作-它創建了一個新的加載程序,但實際上並未啟動加載程序。 啟動加載的唯一方法是通過initLoader
(僅在給定ID不存在時為它創建一個Loader)或restartLoader
(將給定ID丟棄任何現有的Loader並創建一個新的Loader)。
在你的情況下,它看起來像你應該調用restartLoader(MATCH_LOADER_ID, null, this)
在你結束setMatchesOfTheDay
重建與新選定的日期為您的裝載機。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.