简体   繁体   中英

android - Fragment does not get arrayList from Activity

I've a MainActivity that provides a ViewPager, and in this Activity there's a method that fills an ArrayList once app has obtained permissions.
I pass this ArrayList with a setter but the Fragment does not consider the value passed

MainActivity.java

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(context, "Permission granted", Toast.LENGTH_SHORT).show();           
                mArraySongs = listAllSongs();
                listFragment.setSongsList(mArraySongs);
                Log.d("test arraylist in activity: ", Integer.toString(mArraySongs.size()));
            } else {
                Toast.makeText(context, "Permission denied", Toast.LENGTH_SHORT).show();          
            }
            break;
    }
}

ListFragment.java

public void setSongsList(ArrayList<Song> songsList) {
    this.mSongList = songsList;
    Log.d("test fragment setSongsList", Integer.toString(mSongList.size()));
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    mInflater = inflater;
    mContext = container.getContext();
    mContainer = container;
    Log.d("test fragment onCreateView", Integer.toString(mSongList.size()));

    //check if ArrayList is empty
    if(mSongList == null || mSongList.isEmpty()){
        rootView = mInflater.inflate(R.layout.error_no_songs, mContainer, false);
    } else showListView();
    return rootView;
}

Logcat

05-06 01:36:50.118 5272-5272/com.sebb.vilify D/test fragment onCreateView: 0
05-06 01:36:50.817 5272-5272/com.sebb.vilify D/test fragment setSongsList: 7
05-06 01:36:50.817 5272-5272/com.sebb.vilify D/test arraylist in activity: 7

How can I take the value passed with that setter method?

从setSongList调用showListView方法。

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