prevent extra click on customdialogbox - android

I have one customdialog box which will appear on clicking the row of listview.And the data will display on customdialogbox from web service.Now ,if by mistake if i click twice, the customdialog box open twice.My custom dialog box is in Async class and i have call it on onPostexecute().
Every time i click on row of listview -it should called Async class in which customdialog box is there.
How to avoid to open it twice?
Please help.
I have checked some other condition but it is not working for me.
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();
}
}
and in onPost method i have called it:
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;
}
}
}
This async class i am calling in other class where my actual listview is :
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();

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
}
}

Instead of try to avoid twice call of showCustomDialog() method try to avoid start AsyncTask again if task is already running on ListView row click. you can do this using 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
}
}

You should not send a request to the server when item is clicked twice.
Either show a progress dialog in between while first request is sent that will prevent user from clicking at the list item again.
OR check in onItem click like #ρяσѕρєя K said just to add to that use following code define APT_CustomRequestResponse object outside of onItemClick and in your activity:
In activity:
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();
}
}
EDIT
In you Async task create a boolean to know if execution is complete:
boolean finished=false;
And in post execute set it to true:
finished=true;
and finally do this in your 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();
}
}

Related

Can you help me with a listener?

I have a function of showDiaglog.
I just want to, when user tap checkbox the diaglog shown, and the spinner also, but I got a problem with listener. Everytime I open settings, the popup show up twice.
First because of listener of spinner then second because one of my setting checked.
I save every changes in that setting.
Can you help me?
selectLanguage
.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
// TODO Auto-generated method stub
saveSettingan(Key_Select, position);
if (position == 0) {
setDefaultLocal();
} else {
setLocal("in");
}
showDialog();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
and this one
case R.id.checkAcak:
saveSettingan(Key_Acak, isChecked);
showDialog();
break;
You could store a boolean which remembers the visibility of the dialog. Your show dialog should include this:
if (isDialogVisible)
return;
isDialogVisible = true;
Then you set a dismiss listener to set isDialogVisible to false.

After item clicked in spinner, Display dialog

Is it possible to have a spinner that displays a dialog when a item is pressed in the list
and is it good code and not too messy. If so could someone show me some code so i can see the make up of this done thank you
You can add an item selected listener on your spinner and then while an item is selected or touched start a dialog. like this --
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Here add the Dialog you want !", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Android: TabActivity/TabHost with ActivityGroup : Error occurs when creating PreferenceActivity Dialog and Dialog from click button on Custom Listview

I implement a TabActivity (TabHost) with ActivityGroup based on this website http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html
I have 2 questions about implement this.
First question, Using TabActivity with PreferenceActivity, error occurs when I click an preference item to make a preference dialog.
08-10 21:55:35.919: E/AndroidRuntime(1415): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord#405296f0 is not valid; is your activity running?
I found a solution that I should use "getParent()" instead of "getApplicationContext()"or"this" in my code, but I don't know where/how to do it? I don't know how to do something like "setContext(getParent())" with my PreferenceActivity.
I have a problem exactly same as this: TabActivity with ActivityGroup and PreferenceActivity child
Here is my code:
public class MoreUserProfile extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.more_userprofile);
}
In my opinion, I think I should implement something in a dialog class, in onCreateDialogView() because android calls this method at making dialog time. I try my best but I can't solve my problem
public class MoreUserProfileDateOfBirthDialog extends DialogPreference {
LinearLayout layout;
SharedPreferences.Editor editor;
public MoreUserProfileDateOfBirthDialog(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
setDialogLayoutResource(R.layout.more_userprofile_dialog_dateofbirth);
}
#Override
protected View onCreateDialogView() {
// TODO Auto-generated method stub
// I think I should implement some code here.
//Activity parent = new Activity();
//LayoutInflater inflater = (LayoutInflater) parent.getLayoutInflater();
//layout = (LinearLayout) inflater.inflate(R.layout.more_userprofile_dialog_dateofbirth, null);
}
protected void onBindDialogView(View view) {
layout = (LinearLayout) view.findViewById(R.id.more_userprofile_dialog_dateofbirth_linearlayout);
SharedPreferences pref = getSharedPreferences();
int day = pref.getInt("dayOfBirthday", 1);
int month = pref.getInt("monthOfBirthday", 1);
int year = pref.getInt("yearOfBirthday", 2000);
DatePicker dp = (DatePicker) view.findViewById(R.id.more_userprofile_dialog_choosedateofbirth_datePicker);
dp.init(year, month-1, day, onDateChangedListener);
layout.removeAllViews();
layout.addView(dp);
super.onBindDialogView(view);
}
DatePicker.OnDateChangedListener onDateChangedListener = new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
editor = getEditor();
editor.putInt("dayOfBirthday", view.getDayOfMonth());
editor.putInt("monthOfBirthday", view.getMonth()+1);
editor.putInt("yearOfBirthday", view.getYear());
}
};
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
super.onClick(dialog, which);
if(which==DialogInterface.BUTTON_NEGATIVE){
dialog.dismiss();
}
if(which==DialogInterface.BUTTON_POSITIVE){
editor.commit();
}
}
}
Second question, Similar as Above, but this time is about TabActivity with ListView Adapter (Use for Custom Listview). On each Custom Listview item has 2 buttons, "Edit" and "Delete". When I click on one of these button, dialog appears. But after I apply TabActivity to my application, I got an error when press these button. The error code is similar as above:
08-10 22:13:56.643: E/AndroidRuntime(1427): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord#40526c50 is not valid; is your activity running?
Here is my code:
public class ActivityListviewAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context context;
private ArrayList<Activities> listData = new ArrayList<Activities>();
public ActivityListviewAdapter(Context context,ArrayList<Activities> listData,) {
// TODO Auto-generated constructor stub
super();
this.context = context;
this.mInflater = LayoutInflater.from(context);
this.listData = listData;
}
public int getCount() {
// TODO Auto-generated method stub
return listData.size(); //ส่งขนาดของ List ที่เก็บข้อมุลอยู่
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ActivityRowList activitylist;
if(convertView == null){
convertView = mInflater.inflate(R.layout.more_customizedactivity_customlistview, null);
activitylist = new ActivityRowList();
activitylist.ActivityName = (TextView) convertView.findViewById(R.id.TextView_More_Customizedactivity_Customlistview_ActivityName);
activitylist.ActivityDetail = (TextView) convertView.findViewById(R.id.TextView_More_Customizedactivity_Customlistview_ActivityDetail);
activitylist.ActivityType = (ImageView) convertView.findViewById(R.id.ImageView_Activity_ShowallActivity_Customlistview);
activitylist.EditCustomizedButton = (Button) convertView.findViewById(R.id.Button_More_Customizedactivity_Customlistview_Edit);
activitylist.DeleteCustomizedButton =(Button)convertView.findViewById(R.id.Button_More_Customizedactivity_Customlistview_Delete);
convertView.setTag(activitylist);
}else{
activitylist = (ActivityRowList) convertView.getTag();
}
activitylist.ActivityName.setText(listData.get(position).getActivityName());
activitylist.ActivityDetail.setText("MET: "+listData.get(position).getActivityMET().toString());
activitylist.ActivityType.setImageResource(R.drawable.activity_type_customized);
activitylist.EditCustomizedButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
editNewActivityDialog(listData.get(position).getActivityName(), listData.get(position).getActivityMET(),position);
}
});
activitylist.DeleteCustomizedButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
confirmDeleteCustomizedDialog(listData.get(position).getActivityName(),listData.get(position));
}
});
return convertView;
}
private void confirmDeleteCustomizedDialog(final String ActivityNameInput,final Activities listDataposition){
// TODO Auto-generated method stub
AlertDialog alertDialog = new AlertDialog.Builder(mInflater.getContext()).create();
alertDialog.setTitle("Confirm Delete");
alertDialog.setMessage("Do you want to delete:\"" + ActivityNameInput +"\"" );
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,"Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Some codes here
});
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
alertDialog.show();
}
private void editNewActivityDialog(final String ActivityNameBefore, float ActivityMETBefore,final int ListPosition) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(mInflater.getContext());
dialog.setContentView(R.layout.more_customizedactivity_dialog_addnew);
final EditText EditText_Addnewactivity_Dialog_ActivityName=(EditText)dialog.findViewById(R.id.EditText_More_Customizedactivity_Addnewactivity_Dialog_ActivityName);
final EditText EditText_Addnewactivity_Dialog_ActivityMET=(EditText)dialog.findViewById(R.id.EditText_More_Customizedactivity_Addnewactivity_Dialog_ActivityMET);
final Button Button_More_Customizedactivity_Addnewactivity_Dialog_OK=(Button)dialog.findViewById(R.id.Button_More_Customizedactivity_Addnewactivity_Dialog_OK);
final Button Button_More_Customizedactivity_Addnewactivity_Dialog_Cancel=(Button)dialog.findViewById(R.id.Button_More_Customizedactivity_Addnewactivity_Dialog_Cancel);
dialog.setTitle("Edit Activity by User");
EditText_Addnewactivity_Dialog_ActivityName.setText(ActivityNameBefore);
EditText_Addnewactivity_Dialog_ActivityMET.setText(ActivityMETBefore+"");
dialog.show();
Button_More_Customizedactivity_Addnewactivity_Dialog_OK.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// Some codes here
});
Button_More_Customizedactivity_Addnewactivity_Dialog_Cancel.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
}
}
According to my code:
When I press "Edit" on my custom listview, code runs editNewActivityDialog() method and builds Dialog by using this:
final Dialog dialog = new Dialog(mInflater.getContext());
(I think I should change "mInflater.getContext()" to something else but I don't know.)
When I press "Delete" button on my custom listview, code runs confirmDeleteCustomizedDialog() method and builds AlertDialog by using this:
AlertDialog alertDialog = new AlertDialog.Builder(mInflater.getContext()).create();
(I think I should change "mInflater.getContext()" to something else but I don't know.)
Very thanks in advance :)

How to handle an onClickListener for a Button within the Spinner onItemSelected(...) method?

I have a Spinner.onItemSelected() method and I want to be able to have an event happen once a Button is clicked after selecting the Spinner item.
For example if you select Beginner for your Spinner and click Java for another spinner. Under that I have a button that says Start. How do I have a Button.onClick event correspond with the selected spinner options?
I did something like this but what I assigned as the setOnClickListener() value is not being read by the View.OnClickListener.
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selected = (Integer) arg0.getItemAtPosition(0);
position = spinner.getSelectedItemPosition();
start = (Button)findViewById(R.id.start);
start.setOnClickListener(phaseHandler);
View.OnClickListener phaseHandler = new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
}
The phaseHandler I declared is not being read by start.setOnClickListener(phaseHandler) so this results in my View.OnCLickListenr invocation not working because phaseHandler is not being set to the Button start. In Eclipse my phaseHandler has the red underlining curly for an error where it says start.setOnClickListener(phaseHandler);
Any Ideas?
move start.setOnClickListener(phaseHandler); down
this may help you
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selected = (Integer) arg0.getItemAtPosition(0);
position = spinner.getSelectedItemPosition();
start = (Button)findViewById(R.id.start);
View.OnClickListener phaseHandler = new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
start.setOnClickListener(phaseHandler);
}

trying to implement listview inside onclick function

public class CompanySearchActivity extends RathbonesActivity {
private CompanySearchAdapter mStockListAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.companysearch_layout);
final EditText keywordET = (EditText)findViewById(R.id.codeET);
final Button search = (Button)findViewById(R.id.button_stock_add);
final Activity a= CompanySearchActivity.this;
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String keyword = keywordET.getText().toString();
Log.i("keyword: ",keyword);
ArrayList codearr = getResults(keyword);
mStockListAdapter = new CompanySearchAdapter(a,codearr);
ListView listview = (ListView) findViewById(R.id.stocklist);
listview.setAdapter(mStockListAdapter);
listview.setOnItemClickListener(this);
listview.setOnItemLongClickListener(this);
}
});
}
The lines listview.setOnItemClickListener(this);
listview.setOnItemLongClickListener(this); are giving errors because of this keyword, i replaced it with 'a' too but it doesnt work. What can be the possible way to achieve this?
If you wish to use the parent activities onClick method your activity must implement OnItemClickListener and OnItemLongClickListener
public class CompanySearchActivity extends RathbonesActivity implements OnItemClickListener, OnItemLongClickListener
{
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
// TODO Auto-generated method stub
return false;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
// TODO Auto-generated method stub
}
}
Note the code "implements OnItemClickListener, OnItemLongClickListener"
This is vital for implementing it this way.
Then you can call:
listview.setOnItemClickListener(CompanySearchActivity.this);
listview.setOnItemLongClickListener(CompanySearchActivity.this);
The this keyword is a reference to the object that owns the currently executing method. In this case this refers to the anonymous View.OnClickListener object that you are defining. Try replacing this with CompanySearchActivity.this
your listview onClick definition should look like your search listener.
search.setOnClickListener(new View.OnClickListener() {
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String keyword = keywordET.getText().toString();
Log.i("keyword: ",keyword);
ArrayList codearr = getResults(keyword);
mStockListAdapter = new CompanySearchAdapter(a,codearr);
}
});
ListView listview = (ListView) findViewById(R.id.stocklist);
listview.setAdapter(mStockListAdapter);
listview.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
<do somthing when its clicked>
}
});
also ensure if you have multiple layouts that these view items (listview and search)
are in companysearch_layout.xml

Categories

Resources