我有一个自定义对话框,它会在点击listview的行时显示。数据将显示在web服务的customdialogbox上。现在,如果我错误地点击两次,自定义对话框打开两次。我的自定义对话框是在Async类中,我在onPostexecute()
上调用它。
每次我点击listview行时,都应该调用Async类,其中有自定义对话框。
如何避免两次打开?
请帮忙。
我已经检查了一些其他条件,但它对我不起作用。
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 :(得分: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
}
}
答案 1 :(得分:0)
如果任务已经在ListView行上运行,请尝试避免两次调用showCustomDialog()
方法,而不是尝试避免再次启动AsyncTask
。您可以使用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
}
}
答案 2 :(得分:0)
单击两次项目时,不应向服务器发送请求。
在发送第一个请求时显示一个进度对话框,阻止用户再次点击列表项。
或者签入onItem点击就像@ρяσѕρєяK表示只是添加到使用下面的代码定义onTtemClick之外的APT_CustomRequestResponse对象和你的活动:
活动:
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;
在post执行中将其设置为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();
}
}