0

How to call from public static void to not static public void in Fragment class? Or is there another way to call between (listViewHolder.dot.setOnClickListener(new View.OnClickListener() and public void search3() )

enter image description here // Tab2 // public class Tab2 extends Fragment

public void search3() {

Toast.makeText( getActivity(),"search3333333: " ,Toast.LENGTH_SHORT ).show();

}

public static void search4 (Context context1,String text) {

Toast.makeText( context1,text,Toast.LENGTH_LONG ).show();

Tab2 someClass = new Tab2();

someClass.search3();

}
//CustomAdapter 
//public class CustomAdapter extends BaseAdapter

    listViewHolder.dot.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

Toast.makeText( context,"" +  ff.getText().toString() ,Toast.LENGTH_LONG).show();

Tab2.search4(context,"hi hhhh");

        }
    });
Yaroslav Boichuk
  • 1,733
  • 19
  • 29
TARIQ ALEED
  • 69
  • 3
  • 11
  • 1
    Possible duplicate of [I want to know the difference between static method and non-static method](https://stackoverflow.com/questions/3903537/i-want-to-know-the-difference-between-static-method-and-non-static-method) – Ivar Jun 13 '17 at 13:49
  • What's wrong with your picture? It's _technically_ valid, but are you wanting to use a previous instance that was made? – Rogue Jun 13 '17 at 13:52
  • thanks for the help But it's invalid , When come to someClass.search3(); give error and closes the Activity – TARIQ ALEED Jun 13 '17 at 14:32
  • Tab2 is a fragment right? You cannot use `getActivity()` inside `search3()` method unless you commit the fragment to the activity. – Hussain Jun 13 '17 at 15:33
  • Thank you very much. may be This solution useful in this case , and I am a beginner in Android programming I've got the solution – TARIQ ALEED Jun 14 '17 at 15:43

2 Answers2

0

If you want to call Fragment or Activity method from your OnClickListener, you have to store a reference to that Fragment or Activity somewhere. Probably something like this will work for you:

public class CustomAdapter extends BaseAdapter {
    private final Tab2 tab2;
    // other fields

    public CustomAdapter(Tab2 tab2, /* other constructor params ... */ ) {
        this.tab2 = tab2;
        // process other params and other initialization
    }

    ...
    @Override
    public View getView (int position, View convertView, ViewGroup parent) {

        ...

        listViewHolder.dot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tab2.search3();
            }
        });

        ...
    }
}
SergGr
  • 22,842
  • 2
  • 25
  • 49
  • Thank you very much. may be This solution useful in this case , and I am a beginner in Android programming I've got the solution – TARIQ ALEED Jun 14 '17 at 15:42
0

put public class CustomAdapter extends BaseAdapter inside tab2 Fragment class like this way class CustomAdapter extends BaseAdapter and remove static

Thank you very much to all

TARIQ ALEED
  • 69
  • 3
  • 11
  • Tariq, your solution is essentially the same as mine but provides less flexibility. When you put `CustomAdapter` class inside `Tab2` class i.e. make it non-static `inner class` or `nested class`, compiler implicitly adds `tab2` argument to the constructor of `CustomAdapter` and adds it as an argument to the corresponding calls. See [Nested Classes](https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) for some details. – SergGr Jun 14 '17 at 17:08