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
}
Related
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.
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();
}
}
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
}
});
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) {
}
});
I am able to call webservices now.
But In my project i want to populate the name of sponser corresponding to the sponser id .
For doing this I want to call a webservice on text changed listener of sponserid edittext or i have to monitor the focus changed of edittext.
Please guide me how to call webservice and populate another edittext.
Should i take keyevent ???
The codes i have tried so far are :
txtSpnID.setFocusable(true);
if(txtSpnID.isFocused()){
Log.v("TAG", "focus is on txtspid");
}else{
Log.v("TAG", "focus lost from txtspid");
}
This prints focus lost from txtspid on logcat.
if(txtSpnID.hasFocus()){
Log.v("TAG", "focus is on txtspid");
}else{
Log.v("TAG", "focus lost from txtspid");
}
this also prints the same
txtSpnID.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
Log.v("textfield","On text changed");
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
Log.v("textfield","before text changed");
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
Log.v("textfield text",txtSpnID.getText().toString());
Toast.makeText(Registration.this, "SponserID is"+txtSpnID.getText().toString(),
Toast.LENGTH_LONG);
if (txtSpnID.getText().length() == 0) {
Toast.makeText(Registration.this, "Enter SponserID ",
Toast.LENGTH_LONG);
} else {
String spnId = txtSpnID.getText().toString();
try {
//call webservice here and get response from the service
} catch (Exception e) {
}
}
}
});
this prints corresponding logs on logcat
but it does not show toast :(
please help me guys
you just made Toast ... but you did'nt show it ... use Toast.show() like this Toast.makeToast(.....).show();
.... remeber to call web service in different thread(to prevent ANR)