Progress Dialog没有显示我已经尝试了所有可能的解决方案但没有任何工作

时间:2018-04-06 09:42:11

标签: java android

进度对话框没有显示我已经尝试了所有可能的解决方案但没有任何效果

这是我的 java代码,我正在创建 ProgressDialog 的对象并对其进行初始化但仍然在我的活动中不可见 我是否正在开始服务OnCreate也许它会影响我的ProgressDialog是不是?

    public class MainActivity extends AppCompatActivity {
    private ProgressDialog pDialog;
    private ArrayList<CoinDetail> arrayList;
    private FloatingActionButton fab_Coin;
    private ListView coin_details_listView;
    private CoinDetailsAdapter adapter;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading...");
        pDialog.setCancelable(false);
      pDialog.show();
    toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitleTextColor(Color.WHITE);
        getSupportActionBar().setTitle("Crypto Live");

        Intent i = new Intent(MainActivity.this, ServiceCoin.class);
        startService(i);

        /*mInterstitialAd = newInterstitialAd();    //For ad
        loadInterstitial();*/

        coin_details_listView = findViewById(R.id.lv_coinName_mainActivity);
        fab_Coin = findViewById(R.id.fab_add_coin);

        arrayList = new ArrayList<>();
        adapter = new CoinDetailsAdapter(MainActivity.this, arrayList);
        coin_details_listView.setAdapter(adapter);
        CoinSingleton.getInstance().setContext(this);
        adapter = CoinSingleton.getInstance().getCoinDetailsAdapter(pDialog);
        coin_details_listView.setAdapter(adapter);
       coin_details_listView.setTextFilterEnabled(true);
        fab_Coin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, FabCoins.class);
                startActivity(intent);
            }
        });

        coin_details_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(MainActivity.this, CoinDetails.class);
                intent.putExtra("SHORT", CoinSingleton.getInstance().getCoinDetails().get(position).getShort());
                startActivity(intent);
            }
        });

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.menu2, menu);
        // Retrieve the SearchView and plug it into SearchManager
         SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
        SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
 searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {


        if (TextUtils.isEmpty(newText)) {
            coin_details_listView.clearTextFilter();
        } else {
            coin_details_listView.setFilterText(newText);
        }
        return true;
    }
});
        return true;
    }
}

CoinSingleton java class

public class CoinSingleton {

    private Context context;
    private static CoinSingleton singleton = null;
    private ArrayList<CoinDetail> coinDetails = new ArrayList<>();
    private CoinDetailsAdapter coinDetailsAdapter;

    public void setCoinDetails(ArrayList<CoinDetail> coinDetails) {
        this.coinDetails = coinDetails;
    }

    /* A private Constructor prevents any other
     * class from instantiating.
     */
    private CoinSingleton() {
    }

    /* Static 'instance' method */
    public static CoinSingleton getInstance() {
        if (singleton == null) {
            singleton = new CoinSingleton();
        }
        return singleton;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void setCoinDetailsAdapter(ArrayList<CoinDetail> details) {
        for (int i = 0; i < details.size(); i++) {
            try {
                coinDetails.get(i).setPrice(details.get(i).getPrice());
                coinDetails.get(i).setMktcap(details.get(i).getMktcap());
            } catch (Exception e) {
                coinDetails.add(details.get(i));
            }
        }
        coinDetailsAdapter.setCoinDetailsArrayList(coinDetails);
        //Toast.makeText(context, "Adapter set"+details.get(0).getPrice()+details.get(0).getShort(), Toast.LENGTH_SHORT).show();
        coinDetailsAdapter.notifyDataSetChanged();
    }

    public CoinDetailsAdapter getCoinDetailsAdapter(ProgressDialog dialog) {
        coinDetailsAdapter = new CoinDetailsAdapter(context, coinDetails);
dialog.dismiss();
        return coinDetailsAdapter;
    }

    public ArrayList<CoinDetail> getCoinDetails() {
        return coinDetails;
    }
}

2 个答案:

答案 0 :(得分:0)

我认为你缺少progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

  ProgessDialog progressDialog = new ProgressDialog(ctx);
        progressDialog.setMessage("Updating...");
        progressDialog.setIndeterminate(true);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);

修改

new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                pDialog = new ProgressDialog(MainActivity.this);
                pDialog.setMessage("Loading...");
                pDialog.setCancelable(false);
                pDialog.show();
            }
        },100);

答案 1 :(得分:0)

问题是你正在通过pDialog并解雇。 dialog.dismiss()删除了您的对话框。

public CoinDetailsAdapter getCoinDetailsAdapter(ProgressDialog dialog) {
    coinDetailsAdapter = new CoinDetailsAdapter(context, coinDetails);
    dialog.dismiss();
    return coinDetailsAdapter;
}

如果要延迟解除对话框,请尝试此代码。您的对话将在3秒后被解除。

public CoinDetailsAdapter getCoinDetailsAdapter(final ProgressDialog dialog) {
    coinDetailsAdapter = new CoinDetailsAdapter(context, coinDetails);

    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            pDialog.dismiss();
        }
    }, 3000);
    return coinDetailsAdapter;
}