简体   繁体   中英

Array from activity to fragment to show list

Good day,

If I declared an arraylist on the main activity and populated default values in the list and I would like to display it on a fragment in a custom list , how would I access my list in the fragment?

Thank you

 users = new ArrayList<Users>();

In the Fragment

// how do I find the arraylist here?
 UserAdapter<User> adapter = new UserAdapter<User> (getContext().user);

In the activity:

Bundle bundle = new Bundle();
// Put the list in a bundle
bundle.putParcelableArrayList("users", users);
YourFragmentClass fragment = new YourFragmentClass();
fragment.setArguments(bundle);

In the fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Get the list
    List<Users> users = getArguments().getParcelableArrayList("users"); 
    return inflater.inflate(R.layout.fragment, container, false);
}

First, create a method in your activity that returns the ArrayList.

public ArrayList<User> getUsers() {
    return users;
}

Then, in your fragment you need to use the method getActivity() to access your activity. From here, you can cast the call to the specific activity to acess its methods. So your call will look something like this:

ArrayList<User> users = ((MainActivity)getActivity()).getUsers();
UserAdapter<User> adapter = new UserAdapter<User>(users);

Edit: I'm going to show another way to do this to avoid coupling the fragment to the activity. In the first example, you're required to use a specific Activity to get the Users ArrayList. This kind of defeats the purpose of a fragment, which is its re-usability. This fragment won't work straight away if you put it in another Activity because it's bound specifically to MainActivity via the casted getActivity() call.

A better way to do this is to create an interface within your fragment and then have whatever Activity it's attached to implement that interface. This will allow you to add the fragment to any activity that implements this interface without changing the code of the fragment.

Our fragment will look like this:

public class ExampleFragment {

    //Create the interface that will be used to communicate with the
    //Activity. For simplicity, we'll just call it UsersProvider.
    //Whichever Activity uses this Fragment will implement this interface.
    public interface UsersProvider {
        public ArrayList<User> getUsers();
    }

    //Our UsersProvider reference. 
    private UsersProvider usersProvider;

    //Here is where we'll set the Activity as our UsersProvider.
    //We're still calling getActivity(), but we're not casting it to
    //any specific Activity, rather we're casting it as the interface.
    @Override 
    public void onAttach(Context context) {
        usersProvider = (UsersProvider) getActivity();
    }

    // onCreate, onCreateView etc... goes here
}

Our Activity will look like this:

public class MainActivity extends Activity implements ExampleFragment.UsersProvider {

    //It's assumed that this is set somewhere else in the acitivty.
    private ArrayList<User> users;

    //onCreate, etc...

    //Implement the method from UsersProvider interface
    @Override 
    public ArrayList<User> getUsers() {
        return users;
    }
}

So now this is set up in a way that your fragment can be used from any Activity without changing the code in your fragment. Just have an activity implement the UsersProvider interface and you can access your Users ArrayList in your fragment by calling

ArrayList<User> users = usersProvider.getUsers();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM