【问题标题】:Problem with progress dialog not progressing Android Studio进度对话框没有进展 Android Studio 的问题
【发布时间】:2021-05-27 10:57:42
【问题描述】:

我的进度对话框有问题。出现进度对话框,但我看不到任何进展。我不知道我做错了什么。这是我的代码:

case R.id.fill_metal_pieces_table:
            MyApplication myApp = ((MyApplication)getActivity().getApplicationContext());
            boolean tablepopulated = myApp.getMetalPiecesTablePopulated();
            if(tablepopulated == false){
                myApp.setMetalPiecesTablePopulated(true);
                progressDialog = new ProgressDialog(getContext());
                progressDialog.setIndeterminate(false);
                progressDialog.setMax(652);
                progressDialog.setMessage("Chargement...");
                progressDialog.setTitle("Remplissage de la table");
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setCancelable(false);
                progressDialog.show();
                new Thread(new Runnable() {
                    public void run() {
                        Looper.prepare();
                        handler = new Handler(Looper.myLooper()){
                            public void handleMessage(Message msg) {
                                super.handleMessage(msg);
                                progressDialog.incrementProgressBy(msg.what);
                            }
                        };
                        Looper.loop();
                        AssetManager am = getContext().getAssets();
                        InputStream is = null;
                        try {
                            is = am.open("catalogue.xls");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        Workbook wb = null;
                        try {
                            wb = Workbook.getWorkbook(is);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (BiffException e) {
                            e.printStackTrace();
                        }
                        Sheet sheet = wb.getSheet(0);
                        DBAdapter db = new DBAdapter(getContext());
                        MetalPiece metalPiece;
                        for(int row = 1; row <= 652; row ++){
                            String typeMetalPiece = sheet.getCell(0, row).getContents();
                            String weightMetalPiece = sheet.getCell(1, row).getContents();
                            metalPiece = new MetalPiece();
                            metalPiece.setType(typeMetalPiece);
                            metalPiece.setWeight(Float.parseFloat(weightMetalPiece));
                            metalPiece.setFamily("Profile");
                            metalPiece.setUnit("Métre");
                            db.saveMetalPiece(metalPiece);
                            handler.sendEmptyMessage(1);
                        }
                        progressDialog.dismiss();
                        intent = new Intent(getContext(), MainActivity.class);
                        intent.putExtra("tab","metalpiece");
                        startActivity(intent);
                    }
                }).start();
            }
            else{
                Toast statusMetalPiecesTable = Toast.makeText(getContext(), "La table des piéces métalliques est déja remplie",Toast.LENGTH_LONG);
                statusMetalPiecesTable.show();
            }
            return true;

我正在编写代码以使用 excel 文件中的一些数据填充表格。你能帮我解决这个问题吗?非常感谢。

【问题讨论】:

    标签: java android sqlite


    【解决方案1】:

    尝试用new Handler(Looper.getMainLooper()) 替换new Handler(Looper.myLooper()) 或在Thread 之外创建它 - 因为progressDialog 应该/必须在主UI 中更新Thread,而不是一些自定义Thread 与自己的循环

    另外:使用Log.i("some tag", "some value"); 记录步骤并找出哪些部分代码没有正确执行(Handler 甚至会收到这些消息以重定向到progressDialog?放入handleMessage 方法Log.i("some tag", "handleMessage msg.what:" + msg.what);) .日志显示在 IDE 底部的 Android Studio Logcat 选项卡中

    【讨论】:

    • 我尝试了这两种解决方案,但都不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多