How do I call the method Show_list() in another class when the method is declared inside main activity?
The process list in below:
In first : initialize in MainActivityFragment
MainActivity mActivity= new MainActivity();
And then you can continue
mActivity.Show_list(); in MainActivityFragment
MainActivityFragment
The code of Mainactivityfragment
public class MainActivityFragment extends Fragment { MainActivity mActivity= new MainActivity(); public MainActivityFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mActivity.show_list(); return inflater.inflate(R.layout.fragment_main, container, false); }
MainActivity
The code of Mainactivity
public View show_list() { String[] data = {"Ionut", "Eugeniu", "George", "Felix", "Fazlidin"}; List<String> data_list = new ArrayList<>(Arrays.asList(data)); ArrayAdapter<String> data_adapter = new ArrayAdapter<>(this, R.layout.list_item, data_list); ListView data_view = (ListView) this.findViewById(R.id.list_view); data_view.setAdapter(data_adapter); return data_view; }