After item clicked in spinner, Display dialog - android

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

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.

spinner android set selection alternative

I created 2 Spinners and I filled them with an array of numbers. What I want is that the user chooses the number they want on the first one, and then I save it to a variable that I use to populate the dropdown list of the second one with values that start after that number. How can I do this? How can I change the text in the second spinner to start with the number chosen.
I have this, I store the item of the Spinner on the variable x, and I want a second Spinner to start with that variable.
mySpinner.setAdapter(new ArrayAdapter<String>(NewGuiaActivity.this, android.R.layout.simple_spinner_dropdown_item,
establist));
// Spinner on item click listener
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
// Locate the textviews in activity_main.xml
arg0.setSelection(5);
x = estab.get(position).getID();
Toast.makeText(getApplicationContext(), x, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
the arg0.setSelection(5), does the trick but it dont let me choose another value when i click on a diferent item of the spinner how can i change this.

prevent extra click on customdialogbox

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

List item having button

I am working on Android application.
I have a ListView. One of the item has a button, and others have simple text views. I have now requirement of showing button as pressed when it is pressed. I am able to do this.But when I connect the device via. Bluetooth keyboard and navigate through the list. I see list items being selected as expected.But I want the item with the button to look as if button has been selected (focused).
Thanks
Krishna
use
list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Android listfragment onItemClick not working ?

I have a ListFragment, and i want to use onitemclick event. But i have many problems. can you help me ? I put event onstart is this right or correct?
Thanks in advance. ..
public class MesajFragment extends android.support.v4.app.ListFragment
...
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), arg2 + " okwww ",
Toast.LENGTH_LONG);
}
});
}
You aren't calling .show() on your toast message. It should look like this:
Toast.makeText(getActivity(), arg2 + " okwww ",
Toast.LENGTH_LONG).show();
See the .show() at the end? Your onClick is likely working. It just doesn't appear to be since your Toast message isn't showing
Your example gets the ListView and sets its click listener. When extending ListFragment, you need to override onListItemClick instead:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Handle item click
}

Categories

Resources