johnbob
johnbob

Reputation: 57

onClick event Fragment

I have some issues with onClick event in a Fragment. I make Tab-Navigation in the Actionbar and with an TabListener the fragments change. This works fine.

My problem is that a ''normal'' OnClickListener (see below) does not work. I've tried a lot of different things.Here is the current status:

   package com.artifact.actionbarfragments;

   import android.app.ActionBar;
   import android.app.ActionBar.Tab;
   import android.app.Activity;
   import android.app.Fragment;
   import android.app.FragmentTransaction;
   import android.os.Bundle;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.widget.Button;
   import android.widget.CheckBox;
   import android.widget.TextView;


    public class ActionBarFragmentsActivity extends Activity {
    /** Called when the activity is first created. */
    public OnClickListener listener;

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

    // lädt die ActionBar
    ActionBar actionbar = getActionBar();
    // ActionBar.LayoutParams(0);
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionbar.setDisplayShowHomeEnabled(false);
    actionbar.setDisplayShowTitleEnabled(false);
    actionbar.setDisplayShowCustomEnabled(false);

  //   actionbar.setCustomView(R.layout.actionbarview);

    // lädt die Tabs in die Actionbar und setzt ''eigene'' Icons
        ActionBar.Tab TabA =              actionbar.newTab().setIcon(R.drawable.ic_action_view_as_list);
    ActionBar.Tab TabB = actionbar.newTab().setIcon(R.drawable.ic_action_directions);
    ActionBar.Tab TabC = actionbar.newTab().setIcon(R.drawable.ic_action_group);
    ActionBar.Tab TabD = actionbar.newTab().setIcon(R.drawable.ic_action_person);

    // erstellt neue Fragmente
    Fragment fragmentA = new fragmentA();
    Fragment fragmentB = new fragmentB();
    Fragment fragmentC = new fragmentC();
           Fragment fragmentD = new fragmentD();

    // Listener werden angelegt
    TabA.setTabListener(new MyTabsListener(fragmentA));
    TabB.setTabListener(new MyTabsListener(fragmentB));
    TabC.setTabListener(new MyTabsListener(fragmentC));
            TabD.setTabListener(new MyTabsListener(fragmentD));

    // Tabs werden in die actionbar geladen
    actionbar.addTab(TabA);
    actionbar.addTab(TabB);
    actionbar.addTab(TabC);
            actionbar.addTab(TabD);

}

  }


 class  NotfallButtonListener extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
     setContentView(R.id.fragment_container);
     final Button notfallbtn = (Button) findViewById(R.id.notfallbtn);
     notfallbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            notfallbtn.setText("Notfall ausgelöst");
            System.out.println("Notfall Botton gedrückt");
        }
    });

 }
}

When the above process did not work, i tried keeping the onClick classes in the fragment:

    public class fragmentA extends Fragment
        implements View.OnClickListener
    {
     Button notfallbtn;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // das Layout fuer dieses Fragment laden
        return inflater.inflate(R.layout.fragmenta, container, false);

       // Button notfallbtn = (Button) findViewById(R.id.notfallbtn);
        // notfallbtn.setOnClickListener(this);
    }

    @Override
   public void onClick(View view) {
        class  NotfallButtonListener extends Activity {
            @Override
            public void onCreate(Bundle savedInstanceState){
                super.onCreate(savedInstanceState);
                setContentView(R.id.fragment_container);
                final Button notfallbtn = (Button) findViewById(R.id.notfallbtn);
                notfallbtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        notfallbtn.setText("Notfall ausgelöst");
                        System.out.println("Notfall Botton gedrückt");
                    }
                });

             }
         }
      }
     }

Any help will be greatly appreciated ! Thanks a lot!

Upvotes: 1

Views: 1569

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

Change as below considering the button is in fragmneta.xml.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // das Layout fuer dieses Fragment laden
        View view= inflater.inflate(R.layout.fragmenta, container, false);
        // inflate layout
        Button notfallbtn = (Button) view.findViewById(R.id.notfallbtn);
        // initialize button using the inflated view object
        notfallbtn.setOnClickListener(this);
        // listener for button
        return view;   // return inflated view
    }

Then you already have

public class fragmentA extends Fragment
        implements View.OnClickListener

So

@Override
public void onClick(View v)
{
   switch(v.getId())
   {
       case R.id.notfallbtn:
                  // do something
       break;
   }  
} 

Remove all these

@Override
   public void onClick(View view) {
        class  NotfallButtonListener extends Activity {

Upvotes: 3

Related Questions