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

时间:2017-11-21 14:51:02

标签: android android-layout android-alertdialog android-dialog

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

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

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

}

我能够在找到它时在警告对话框中显示路径。但是当尺寸为零时无法显示。这是截图

File name dispalyed image

Empty message image when file not found

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

1 个答案:

答案 0 :(得分:2)

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

TextUtils.isEmpty(csvFiles);

如果返回false,则以这种方式构建对话框

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

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