gpt4 book ai didi

java - Toast 重复显示两次

转载 作者:行者123 更新时间:2023-11-30 00:00:54 26 4
gpt4 key购买 nike

当我按下刷新按钮时, toast 重复出现两次。

我只实现了一次 toast。跟适配器有关系吗?

这是代码-

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);
}
}
});

关于java - Toast 重复显示两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49955583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com