codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Create own OnItemClickListener


By : , Category : android

You create an interface first, then implement a method, where you set the listener in your custom class and already then make a main class to listen for your custom event

Interface:

public interface OnCustomEventListener{
  public void onEvent();   //can have parameters
}

method in your e.g. adapter: private OnCustomEventListener mListener; //field

//setter method
public void setCustomEventListener(OnCustomEventListener eventListener) {
   this.mListener=eventListener;
}

listener:

someObjectYouWantToHaveYourCustomListenerToBeAssignedFor.setCustomEventListener(new OnCustomEventListener(){
    public void onEvent(){
    //do whatever you want to do when the event is performed.
    }
});  

how to call e.g. from your adapter:

if(this.mListener!=null){
   this.mListener.onEvent();
}

P.S. Your custom listener may have as many parameters as you want

Source

ReLated :

Try this code

@Override
    public void onItemClick(AdapterView<?> paren, View view,
            int position, long id) {
        // TODO Auto-generated method stub
        Log.i("Value", List_Data.get(position));

    }

If I understand you correctly, you want a listener to use when a RadioButton is checked. For this use, onCheckedChangeListener

You can use the second parameter to get the id of the RadioButton that was checked then you can do whatever you need with it from there.

yourRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) 
    { 
        if (checkedId == R.id.some_id)
        {
            // do something
        }

    });

Documents. Or just the example code:

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.pick_color)
           .setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
               // The 'which' argument contains the index position
               // of the selected item
           }
    });
    return builder.create();
}

P.S. add .show(); after the create to immidiate show the dialog.

try this

  private void AlertDialogView() {
    final CharSequence[] items = { "One", "Two", "Three", "Four" };

    AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);//ERROR ShowDialog cannot be resolved to a type
    builder.setTitle("Alert Dialog with ListView and Radio button");
    builder.setSingleChoiceItems(items, -1,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(), items[item],
                            Toast.LENGTH_SHORT).show();
                }
            });

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT)
                    .show();
        }
    });

    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT)
                    .show();
        }
    });

    AlertDialog alert = builder.create();
    alert.show();
}
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .