当 setSingleChoiceItems 大小为零时在警告对话框中显示消息

Display message in alert dialog when setSingleChoiceItems size is zero

我正在处理警报对话框,我正在获取 SD 卡路径并在警报对话框中显示单选按钮。我需要在搜索结果大小为零时显示消息。

这是我用来从 SD 卡获取路径并在警告对话框中显示的代码

CharSequence[] csvFiles = {};

public void dialog() {

    final AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
    alt_bld.setTitle(R.string.credentials);
    alt_bld.setCancelable(false);
    alt_bld.setSingleChoiceItems(csvFiles, -1, new DialogInterface
            .OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

            regionsDialog();
            dialog.dismiss();
        }
    });
    alt_bld.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int i) {
            finish();
        }
    });
    AlertDialog alert = alt_bld.create();
    alert.show();

}

这里是后台取文件

public ArrayList<File> getfile(File dir) {
    File listFile[] = dir.listFiles();
    File sdPathFile = Environment.getExternalStorageDirectory();
    if (listFile != null && listFile.length > 0) {
        for (int i = 0; i < listFile.length; i++) {
            if (listFile[i].getName().endsWith(".csv")) {
                if (listFile[i].getName().equals(Constants.CREDENTIALS)) {
                    fileList.add(listFile[i]);
                    csvFiles = new CharSequence[]{sdPathFile.getAbsolutePath() + "/" + listFile[i].getName()};
                    File yourFile = new File(dir, listFile[i].getName());
                    try {

                        readFileData(yourFile.toString());

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                } else {

                    LoginActivity.this.runOnUiThread(new Runnable() {
                        public void run() {
                             Toast.makeText(LoginActivity.this, "File not found", Toast.LENGTH_SHORT).show();
                        }
                    });

                }

            }
        }
    }
    return fileList;
}

//Find the root of SD CARD and check for CSV
private class BackgroundTask extends AsyncTask<Void, Void, Void> {
    private ProgressDialog dialog;

    public BackgroundTask(LoginActivity activity) {
        dialog = new ProgressDialog(activity);
    }

    @Override
    protected void onPreExecute() {
        dialog.setMessage("Looking for (credentials.csv) files...");
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected void onPostExecute(Void result) {
        if (dialog.isShowing()) {
            dialog.dismiss();
            dialog();

        }
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            root = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath());
            getfile(root);
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return null;
    }

}

当大小为零时found.But无法显示时,我可以在警告对话框中显示路径。这是屏幕截图

请帮我在找不到文件时在对话框中显示消息

您可以使用以下代码段检查 csvFiles 是否为空:

TextUtils.isEmpty(csvFiles);

如果 returns 错误,以这种方式构建对话框

alt_bld.setSingleChoiceItems(csvFiles, -1, new DialogInterface
        .OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {

        regionsDialog();
        dialog.dismiss();
    }
});

否则设置对话框的文本

alt_bld.setText("No item found");

完整功能:

public void dialog() {

final AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setTitle(R.string.credentials);
alt_bld.setCancelable(false);
if(csvFiles.length==0){
    alt_bld.setText("No item found");
} else {
    alt_bld.setSingleChoiceItems(csvFiles, -1, new DialogInterface
        .OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

            regionsDialog();
            dialog.dismiss();
        }
    });
}
alt_bld.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int i) {
        finish();
    }
});
AlertDialog alert = alt_bld.create();
alert.show();
}

EDIT 我没有注意到 csvFiles 是一个数组。检查条件请使用csvFiles.length