吐司重复出现两次

时间:2018-04-21 12:05:33

标签: java android toast

当我按Refresh button时,toast会重复出现两次。

我只实施toast一次。 与adapter有什么关系吗?

这是代码 -

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

我被困在这个问题很多天了。 我试着搜索它,但没有解决我的问题。

1 个答案:

答案 0 :(得分:1)

您在每次刷新时都会启动一个新的加载器,尝试在单击刷新按钮时重新启动相同的加载器:

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