如何从警报对话框中获取选定的值?

时间:2016-10-14 11:41:34

标签: android alertdialog

我正在使用警告对话框来显示一些记录。这些记录是从db传入并在Alertdialog中显示的。当用户点击项目时我想获取项目名称为Log.this是我的code.it显示项目为{{ 1}}但我希望它显示为[test]

在顶部

初始化的ArrayList arrayList和String [] categoryStrings
test

显示日志值 List<Video> vd=Video.findWithQuery(Video.class, "select * from Video"); if (vd.size()>0) { for (Video v : vd) { arrayList.add(v.getTitle()); } final List<String> list = Arrays.asList(arrayList.toString()); categoryStrings=new String[list.size()]; categoryStrings=list.toArray(categoryStrings); AlertDialog.Builder alert = new AlertDialog.Builder(Editmedia.this); alert.setTitle("Media List"); alert.setCancelable(false); final int selected = 0; // or whatever you want alert.setSingleChoiceItems(categoryStrings, selected, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { //onclick String categoryString = categoryStrings[item]; Log.d("sel", " " + item+" "+categoryString); edit(); } }); alert.show(); 我希望它为0 [test]

日志 Log

2 个答案:

答案 0 :(得分:1)

试试这个,它应该有帮助

String categoryString = categoryStrings[item];

categoryString = categoryString.replaceAll("[\\p{Ps}\\p{Pe}]","");
Log.d("sel", " " +" "+categoryString);

[编辑]

String categoryString = categoryStrings[item];

categoryString = categoryString.replace("[","");
categoryString = categoryString.replace("]","");

Log.d("sel", " " +" "+categoryString);

答案 1 :(得分:0)

试试这个:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Media List");
                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1);
                arrayAdapter.clear();
                for (int i = 0; i < list.size(); i++) {
                    Log.i(LOG_TAG, list.get(i));
                    arrayAdapter.add(list.get(i));
                }
                builder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), list.get(which),
                                Toast.LENGTH_LONG).show();
                    }
                });
                builder.setPositiveButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();