当我按下刷新按钮时,吐司反复出现两次。
我只实施一次吐司。与适配器有什么关系吗?
这是代码 -
public class EarthquakeActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<ArrayList<EarthquakeDetails>> {
public static final String USGS_URL;
public static ArrayList<EarthquakeDetails> earthquakes;
private boolean refreshed = false;
ListView earthquakeListView;
EarthquakeAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.earthquake_activity);
earthquakeListView = (ListView) findViewById(R.id.list);
getSupportLoaderManager().initLoader(0, null, EarthquakeActivity.this).forceLoad();
}
@Override
public Loader<ArrayList<EarthquakeDetails>> onCreateLoader(int id, Bundle args) {
Builder uriBuilder;
<building of url>
for (int i= 0; i < Integer.parseInt(limit); i++) {
maps.add(null);
}
return new EarthquakeAsyncTaskLoader(EarthquakeActivity.this, uriBuilder.toString());
}
@Override
public void onLoadFinished(Loader<ArrayList<EarthquakeDetails>> loader, ArrayList<EarthquakeDetails> data) {
earthquakes = data;
adapter = new EarthquakeAdapter(EarthquakeActivity.this, earthquakes);
if (!isConnected || earthquakes == null) {
<do something>
} else if (earthquakes.size() < 1 || earthquakes.get(0) == null) {
<do something else>
} else {
earthquakeListView.setAdapter(adapter);
if(refreshed){
Toast.makeText(getApplicationContext(), "Refreshed", Toast.LENGTH_SHORT).show();
}
findViewById(R.id.progress).setVisibility(View.GONE);
findViewById(R.id.list).setVisibility(View.VISIBLE);
findViewById(R.id.went_wrong).setVisibility(View.GONE);
}
findViewById(R.id.refresh).setVisibility(View.VISIBLE);
refreshed = true;
findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(findViewById(R.id.list).getVisibility() == View.GONE||findViewById(R.id.list).getHeight() <= 0){
<do something>
}
getSupportLoaderManager().initLoader(0, null, EarthquakeActivity.this).forceLoad();
}
});
}
}
每次刷新时都会启动一个新的加载程序,请尝试在单击刷新按钮时重新启动相同的加载程序:
findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(findViewById(R.id.list).getVisibility() == View.GONE||findViewById(R.id.list).getHeight() <= 0){
<do something>
}
if(getSupportLoaderManager().getLoader(0) == null) {
getSupportLoaderManager().initLoader(0, null, EarthquakeActivity.this);
} else {
getSupportLoaderManager().restartLoader(0, null, EarthquakeActivity.this);
}
}
});