OnClick to call interface method from MainActivity

I need to perform an onClick to call the onItemSelected method on a listener of another class. I don't know how to call this method on the Image onClick listener. So it will move to the HomeFirstFragment class.

ItmeSelectedListener

public interface ItemSelectedListener {

    public void onItemSelected(final int position, final String content);
}

      

LayoutActivity.java:

public class LayoutActivity extends Activity implements OnClickListener {

ImageButton btn_click;

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid3);

        btn_click = (ImageButton) findViewById(R.id.btn_click);

   }
    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.btn_click:


             break;
        }
    }

}

      

MainActivity.java:

public class MainActivity extends ActionBarActivity implements OnTabChangeListener,ItemSelectedListener {

 private TextView action_bar_hometext;


 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

}

 @Override
    public void onItemSelected(int position, String content)
    {

        if(position==0)
        {
            action_bar_hometext.setText(content);
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction ft = manager.beginTransaction();
            HomeFirstFragment newFragment = new HomeFirstFragment();
            ft.replace(R.id.realtabcontent, newFragment);
            ft.addToBackStack(null);
            ft.commit();
        }
     }

      

If I click on the btn_click in the LayoutActivity class, I need to execute the onClick method to call the MainActivity interface.

Anyone can help me with this. Thank.

+3


source to share


2 answers


You can create your own listener and add the block of code you want to execute by clicking in your own listener.

Create your interface like

Interface MyListener{
    public void myClickListener(String content);
}

      



Now implement this in your MainActivity

public class MainActivity extends ActionBarActivity implements OnTabChangeListener,ItemSelectedListener,MyListener {
    public void myClickListener(String content){
        action_bar_hometext.setText(content);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        HomeFirstFragment newFragment = new HomeFirstFragment();
        ft.replace(R.id.realtabcontent, newFragment);
        ft.addToBackStack(null);
        ft.commit();
    }
}

      

+2


source


You need to register the class MainActivity

with the class LayoutActivity

for the class LayoutActivity

to call the interface method.

Add this to your LayoutActivity.java

:

private static ItemSelectedListener mListener = null;

public static void register(ItemSelectedListener listener){
    mListener = listener;
}

@Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.btn_click:
            if(mListener!=null){
                // ADD THIS LINE
                mListener.onItemSelected(POS/*Your position*/, CONTENT/*Your content*/);
            }

         break;
    }
}

      



Now, in your class MainActivity

, register it in the class LayoutActivity

and do:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LayoutActivity.register(this);
}

      

Hope this helps! :)

+1


source







All Articles