2011-04-12 32 views
0

如何使用AlertDialog将验证应用于ListView?我的情况是这样的,我需要让用户选择他们从中进行搜索的状态。国家的价值在微调中定义。如果用户在未选择状态的情况下点击Listview项目,我已经在下面定义了一个AlertDialog。具有AlertDialog验证的ListView

我遇到的问题是警报框显示,但不会停止执行基础任务。

在用户从微调器中选择状态之前,如何停止执行我的AsyncTask?是否有任何示例显示如何验证用户是否在启动AsyncTask之前从微调器中进行了选择?

list.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) { 

       // This code executes just fine but runs asynchronous with my SearchProducts task. 
       String selectedState = spinner.getSelectedItem().toString(); 
       if(selectedState.equals("Choose Your State")) { 
        alertbox("State", "Please Choose Your State"); 
       }   

       String strText = items[position]; 

       if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_search))) { 
        new SearchProducts().execute(); 

这是我的AlertDialog代码。

protected void alertbox(String title, String mymessage) { 
     new AlertDialog.Builder(this) 
      .setMessage(mymessage) 
      .setTitle(title) 
      .setCancelable(true) 
      .setNegativeButton(android.R.string.cancel, 
       new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton){} 
       }) 
      .show(); 
     } 
+0

您可以重写它,以便您显示对话框或启动异步任务,而不是显示对话框并每次启动异步任务? – James 2011-04-12 16:46:59

回答

0

我觉得自己像这样一个业余爱好者,我以正确的方式实现了对话。我的代码中缺少的是我的onClick for AlertDialog上的“返回”声明。更新后的代码如下所示。

protected void alertbox(String title, String mymessage) { 
     new AlertDialog.Builder(this) 
      .setMessage(mymessage) 
      .setTitle(title) 
      .setCancelable(true) 
      .setNegativeButton(android.R.string.cancel, 
       new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton){ 
        return; 
       } 
       }) 
      .show(); 
     }