更新对话警报的项目(SingleChoiceItems)

时间:2016-06-02 08:26:47

标签: android alertdialog

我有像下面的对话警告

Dialog Alert

当我关闭它然后更新它的项String Array
在我再次打开它之后,我看到上面较旧的Dialog图片和
没有改变。问题是什么?

这是调用Alert Dialog

的代码
 public void onClickSafheMojud(View view){
    item=null;
    Cursor cursor=dbAdapter.getAllData();
    int i=0;
    if(cursor.moveToFirst()) {

        i=1;
        while (cursor.moveToNext()){

            i++;
        }
    }
    if(i>0) {
        CharSequence[] items = new CharSequence[i];

        if (cursor.moveToFirst()) {
            items[0] = cursor.getString(0);
            i = 1;
            while (cursor.moveToNext()) {
                items[i] = cursor.getString(0);
                i++;
            }
        }
        item=items;
        showDialog(0);
    }
    else


}  

这是Dialog Alert Builder

@Override
protected Dialog onCreateDialog(int id){
    switch (id){
        case 0:

AlertDialog.Builder builder2=new AlertDialog.Builder(this);
            builder2.setIcon(R.drawable.res);
            builder2.setTitle("انتخاب کنید");
            builder2.setPositiveButton("برو", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {


                }
            });
            builder2.setNegativeButton("لغو", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getBaseContext(), "لغو شد.", Toast.LENGTH_SHORT).show();
                }
            });

            builder2.setSingleChoiceItems(item, 0, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    kodam_safhe=which;
                }
            });
            kodam_safhe=0;
            return builder2.create();


 return null;
}

2 个答案:

答案 0 :(得分:0)

采用全球int变量。

int test = 0;

setSingleChoiceItems就像这样

builder2.setSingleChoiceItems(item, test, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    test = which;
                }
            });

快乐编码..

答案 1 :(得分:0)

过了几天我找到答案了 问题是:使用onCreateDialog并覆盖它 我只是创建一个AlertDialog.Builder,然后显示它 我没有使用showDialog()方法
项目每次更新