2011-04-12 4 views
0

AlertDialog를 사용하여 ListView에 유효성 검사를 적용하려면 어떻게해야합니까? 내 시나리오는 사용자가 검색을 수행하는 주를 선택하게해야합니다. 상태의 값은 회 전자에 정의됩니다. 사용자가 상태를 선택하지 않고 Listview 항목을 클릭하면 아래에 AlertDialog가 정의됩니다.AlertDialog Validation이있는 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

답변

0

나는 아마추어처럼 느껴진다. 나는 올바른 방식으로 대화 상자를 구현했다. 내 코드에서 누락 된 것은 AlertDialog에 대한 onClick에 대한 "return"문이었습니다. 업데이트 된 코드는 다음과 같습니다.

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