从另一个活动重新启动对话框

时间:2014-02-20 09:39:57

标签: android android-alertdialog dynamic-programming

我遇到了一个问题,我在一个活动中有一个Dialog,其自定义列表视图是动态生成的,每行有1个textview和1个复选框。

当我按下复选框时,它会打开另一个活动来配置某些内容,但是如果我按下此活动的取消,我希望当它返回到预览活动时,复选框上的检查不存在我取消了它。

那么我如何从主要活动的1个活动重启Dialog?

修改

public void corredialogo()
{       
    CliInfo cliente = GlobalVars.getClientes_Info().get(GlobalVars.get_selected_client()+"");
    if (!cliente.ultartigos.isEmpty())
    {
        LayoutInflater inflater = LayoutInflater.from(context);            
        View vi = inflater.inflate(R.layout.ultimosart, null);          
        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setView(vi);
        AlertDialog OptionDialog = alert.create();
        produtos = (ListView) vi.findViewById(R.id.ultprodutos);
        ArrayAdapter<LastArt> adapter = new UltimosArtAdapter((Activity) context, cliente.getultimosart());         
        produtos.setAdapter(adapter);
        OptionDialog.show();
    }
    else 
    {
        GlobalFunctions.toast_message(getApplicationContext(), getString(R.string.nullUltimos));
    }
}

编辑2

复选框的监听器

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            {
                element = (LastArt) viewHolder.checkbox.getTag();
                GlobalVars.ultiQtd = element.getQtd();
                if (!GlobalVars.get_encomenda_produtos().containsKey(element.getId_Art()))
                {                       
                    if(buttonView.isChecked() && element.getSelected() == false)
                    {
                        GlobalVars.setActive_product_id(element.getId_Art());
                        element.setSelected(buttonView.isChecked());
                        Intent myIntent = new Intent();
                        myIntent.setClass(context, ProdConfig.class);
                        context.startActivity(myIntent);
                    }
                }
            }

第二项活动的取消按钮的监听器

cancelar.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {   
            Activity.prodsearch.setText("");
            GlobalVars.getClientes_Info().get(GlobalVars.get_selected_client()+"").
                deselArt(GlobalVars.getActive_product_id()); // Put false on the prod
            finish();
        }
    });

1 个答案:

答案 0 :(得分:1)

startActivityForResult代替startActivity。然后在第一个活动的onActivityResult中再次显示对话框。

示例:我假设活动名称为FirstActivity和SecondFirstActivity。

     // first, close your dialog
        // calling SecondActivity from FirstActivity on some event i.e checking a checkbox 

        Intent i = new Intent(this, SecondActivity.class);
        startActivityForResult(i, 1); // 1 is your request code

        // In SecondActivity , on cancel pressed

        setResult(RESULT_CANCELED);     
        finish();


   // Again in FirstActivity

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {

         if(resultCode == RESULT_CANCELED){      
             //Write your code in case SecondActivity send CANCEL response  
             corredialogo();// display dialog     
         }
         if (resultCode == RESULT_OK) {    
             //Write your code in case some result has been sent by SecondActivity
         }
      }   

希望它会有所帮助。

=============================================== =====================

这是您修改后的代码。

  AlertDialog OptionDialog=null;
    public void corredialogo()
    {       
        CliInfo cliente = GlobalVars.getClientes_Info().get(GlobalVars.get_selected_client()+"");
        if (!cliente.ultartigos.isEmpty())
        {
            LayoutInflater inflater = LayoutInflater.from(context);            
            View vi = inflater.inflate(R.layout.ultimosart, null);          
            AlertDialog.Builder alert = new AlertDialog.Builder(context);
            alert.setView(vi);
            OptionDialog = alert.create();
            produtos = (ListView) vi.findViewById(R.id.ultprodutos);
            ArrayAdapter<LastArt> adapter = new UltimosArtAdapter((Activity) context,   cliente.getultimosart());         
            produtos.setAdapter(adapter);
            OptionDialog.show();
        }
        else 
        {
            GlobalFunctions.toast_message(getApplicationContext(), getString(R.string.nullUltimos));
        }
    }



//The listener for the checkbox



 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
                {
                    element = (LastArt) viewHolder.checkbox.getTag();
                    GlobalVars.ultiQtd = element.getQtd();
                    if (!GlobalVars.get_encomenda_produtos().containsKey(element.getId_Art()))
                    {                       
                        if(buttonView.isChecked() && element.getSelected() == false)
                        {
                            GlobalVars.setActive_product_id(element.getId_Art());
                            element.setSelected(buttonView.isChecked());
                            Intent myIntent = new Intent();
                            myIntent.setClass(context, ProdConfig.class);
                            context.startActivityForResult(myIntent,1);
                            OptionDialog.cancel();
                        }
                    }
                }


 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

                if (requestCode == 1) {

                 if(resultCode == RESULT_CANCELED){      
                     //Write your code in case SecondActivity send CANCEL response  
                     corredialogo();// display dialog     
                 }
                 if (resultCode == RESULT_OK) {    
                     //Write your code in case some result has been sent by SecondActivity
                 }
              } 

第二项活动的取消按钮的监听器

cancelar.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {   
            Activity.prodsearch.setText("");
            GlobalVars.getClientes_Info().get(GlobalVars.get_selected_client()+"").
                deselArt(GlobalVars.getActive_product_id()); // Put false on the prod
             setResult(RESULT_CANCELED);    
            finish();
        }
    });

我没有在任何IDE中编辑它。对不起,如果你发现一些语法拼写错误。