0

I am beginner in android There are various questions about this topic but not clear for me

I want to pass my List to fragment where I have to it in My custom list Adapter how to do it?

public class MainActivity extends AppCompatActivity {
    List<News> newsList;

    private void selectItem(int position) {
        // update the main content by replacing fragments

        if (isOnline()) {
            newsList = requestData("http://Mywebsite.com/Ilmnews/api/news/getAllNews");
        } else {
            Toast.makeText(MainActivity.this, "Network ins't available", Toast.LENGTH_SHORT).show();
        }
        Fragment fragment = new NewsFragment();
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.fragmentholder, fragment).commit();

    }

    public static class NewsFragment extends Fragment {

        public NewsFragment() {
            // Empty constructor required for fragment subclasses
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_news, container, false);


            ListView newsListView = (ListView) rootView.findViewById(R.id.newsListView);

            // newsList is List of my News object    List<News> 
            newsListView.setAdapter(new MYListAdapter(getActivity(), newsList));

            return rootView;
        }
    }

}

other Examples are just for system data types not for custom objects

Hanzala
  • 1,965
  • 1
  • 16
  • 43
  • Possible duplicate of [Send data from activity to fragment in android](http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android) – Thomas R. Oct 05 '15 at 06:45
  • @ThomasR that is not for custom object as i want – Hanzala Oct 05 '15 at 06:51

1 Answers1

0
public class MainActivity extends AppCompatActivity {
  public static List<News> newsList;
}

And use in your fragment like below

wsListView.setAdapter(new MYListAdapter(getActivity(), MainActivity.newsList));
droidev
  • 7,352
  • 11
  • 62
  • 94