android 吐司重复显示两次

j9per5c4  于 2023-04-10  发布在  Android
关注(0)|答案(1)|浏览(147)

当我按下刷新按钮时,吐司反复出现两次。
我只实现了一次吐司。有什么和适配器有关吗?
密码是-

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

qnzebej01#

你在每次刷新时都要初始化一个新的加载器,当你点击刷新按钮时,试着简单地重新启动同一个加载器:

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

相关问题