2013-12-10 136 views
1

我有一個customdialog框會出現在點擊listview的那一行。並且數據會顯示在web service的customdialogbox上。現在,如果我錯誤地點擊了兩次,customdialog框會打開兩次。我的自定義對話框是在異步類,我已經打電話給onPostexecute()防止額外點擊customdialogbox

每次我點擊一行listview - 它應該叫做Async類,其中customdialog框在那裏。

如何避免打開它兩次?

請幫忙。

我檢查了一些其他情況,但它不適合我。

public void showCustomDialog() { // this is the dialog box i have created 
      // TODO Auto-generated method stub 
      final Dialog dialog = new Dialog(context); 
      final List<String> list=new ArrayList<String>(); 
      list.add("Low"); 
      list.add("Normal"); 
      list.add("High"); 

      dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      dialog.setContentView(R.layout.customdialoguniversalappointment); 
      final ListView listcustomuniversalappt=(ListView) dialog.findViewById(R.id.listcustomuniversalappt); 

      LinearLayout layoutsubject=(LinearLayout) dialog.findViewById(R.id.layoutsubject); 
      LinearLayout layoutappt=(LinearLayout) dialog.findViewById(R.id.layoutappt); 
      Spinner spinnerappt=(Spinner)dialog.findViewById(R.id.permissionspinner); 

      ImageView cancel=(ImageView)dialog.findViewById(R.id.imgcancel); 
      Button cancelappt=(Button)dialog.findViewById(R.id.btncancelappt); 
      Button confirmappt=(Button)dialog.findViewById(R.id.btnconfirmappt); 
      EditText subject=(EditText) dialog.findViewById(R.id.edtsubject); 
        subject.setText("Appointment"); 


        TextView txtdateslot=(TextView) dialog.findViewById(R.id.txtdateslot); 
       txtdateslot.setText("Date:"+DoctorAppointmentAPTrequest.universalapt_date+" Time Slot:"+DoctorAppointmentAPTrequest.universalslot_time);  



      ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context, 
        android.R.layout.simple_spinner_item, list); 
       dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
       spinnerappt.setAdapter(dataAdapter); 

       spinnerappt.setOnItemSelectedListener(new OnItemSelectedListener() { 

        @Override 
        public void onItemSelected(AdapterView<?> arg0, View arg1, 
          int arg2, long arg3) { 
         // TODO Auto-generated method stub 
         int index = arg0.getSelectedItemPosition(); 
        selected_item=arg0.getItemAtPosition(arg2).toString(); 



        } 

        @Override 
        public void onNothingSelected(AdapterView<?> arg0) { 
         // TODO Auto-generated method stub 

        } 
       }); 

       System.out.println("APTCustomRequestResponse.runcustomuniversalappdata="+run_custom_universal_apptdata()); 
      APTRequestCustomAdapter adap=new APTRequestCustomAdapter(context,run_custom_universal_apptdata()); 
       listcustomuniversalappt.setAdapter(adap); 



      cancel.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        dialog.dismiss(); 
        System.out.println("hello"); 
       } 
      }); 

      cancelappt.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        dialog.dismiss(); 
       } 
      }); 
      //confirm appt 
      confirmappt.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        if(APT_CustomRequestResponse.radiostring.equals("checked")){ 
         showdialogforconfirmappointment(); 
        }else{ 
         Toast.makeText(context, "Please Select a Patient to book a Appointment",2000).show(); 
        } 

       } 
      }); 

      getsubject=subject.getText().toString(); 
      if(!dialog.isShowing()||dialog==null){//here i am checking condition where it should appear or not 
      dialog.show(); 

      System.out.println(isMatch); 
      }else{ 
       dialog.dismiss(); 
      } 
     } 

onPost方法我把它叫做:

public class APT_CustomRequestResponse extends AsyncTask<String,String,String>{ 
    protected void onPostExecute(String result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
       showCustomDialog();//here i am calling custom dialog box 

    @Override 
    protected String doInBackground(String... params) { 
     String hunt= GetAPT_RequestCustomdata(); 
     return hunt; 
    }  

     } 
} 

這個異步類我在其他類調用在我的實際列表視圖是:

listview.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { 
    APT_CustomRequestResponse APT=new APT_CustomRequestResponse(MainActivity.this); 
      APT.execute(); 

回答

0
Use below provided code for reference toward solving this issue. 

// Keep this as member variable to the class 
final Dialog dialog = new Dialog(context); 

// your new method model 
public void showCustomDialog() { 
    // Here check this 
    if(dialog != null && dialog.isShowing()){ 
     // Decide if you want to display a new one, if yes then write below code first 
     dialog.dismiss(); 
     dialog = null; 

     // Display a new dialog code here 
    }else{ 
     // Dialog is no visible, diaply it here 
    } 

} 
0

代替儘量避免兩次撥打電話showCustomDialog()方法儘量避免再次啓動AsyncTask如果tas k已經在ListView行點擊上運行。您可以使用AsyncTask.Status做到這一點:

public boolean isfirsttimeclick=true; 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
         long arg3) { 

     APT_CustomRequestResponse APT=new APT_CustomRequestResponse(MainActivity.this); 
     if(isfirsttimeclick || APT.getStatus() == AsyncTask.Status.FINISHED){ 
      APT.execute(); 

     }else{ 
      /// show message to user already running 
     } 

    } 
+0

現在,我看不到結果,控制只會到其他塊 – abh22ishek

+0

@ abh22ishek:您還需要使用一個標誌來啓動AsyncTask時間,當用戶點擊L istView行並使'isfirsttimeclick = true;'onPostExecute方法 –

0

被點擊了兩次項目時,不應發送請求到服務器。

在發送第一個請求時會在兩者之間顯示一個進度對話框,阻止用戶再次單擊列表項。

或檢查onItem單擊像@ρяσѕρєяK說只是要補充的使用下面的代碼定義APT_CustomRequestResponse對象onItemClick外部的,並且您的活動:

在活動:

APT_CustomRequestResponse APT=null; 

onItemClick:

@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
if(MainActivity.this.APT==null||MainActivity.this.APT.getStatus() == AsyncTask.Status.FINISHED){ 
    MainActivity.this.APT=new APT_CustomRequestResponse(MainActivity.this); 
    MainActivity.this.APT.execute(); 
} 


} 

編輯

在您異步任務創建一個布爾知道是否執行完畢:

boolean finished=false; 

,並在後期執行它設置爲true:

finished=true; 

終於做到這一點在你的onItemClick:

@Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
         long arg3) { 
    if(MainActivity.this.APT==null||MainActivity.this.APT.finished){ 
     MainActivity.this.APT=new APT_CustomRequestResponse(MainActivity.this); 
     MainActivity.this.APT.execute(); 
    } 


    } 
+0

謝謝,但它不工作。 – abh22ishek

+0

發生了什麼? –

+0

控制不會在代碼塊 – abh22ishek