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


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

最佳答案

您将在每次刷新时初始化一个新的加载器,单击“刷新”按钮时,只需尝试重新启动同一加载器:

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/

10-14 12:37