可以使用arraylist <customclass>将值从活动传递到片段android

时间:2016-09-16 09:27:55

标签: android android-fragments android-activity parcelable

我正在尝试将自定义ArrayList从活动传递到片段,但不明白代码有什么问题。没有错误,但即便是我也无法获得预期的输出。 在努力修复它近3天之后我完全失去了(是的,但我只是一个初学者)。请帮我解决一下。目的是我想将片段用于另一个活动。

这是我的活动类:

 public class CityDetailsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Create a list of City Details
        ArrayList<CityDetails> cityDetail = new ArrayList<CityDetails>();
        cityDetail.add(new CityDetails("information", "near", "local food", R.drawable.img1, R.drawable.img1, R.drawable.img6));
        Bundle args = new Bundle();
        args.putParcelableArrayList("key", cityDetail);
        CityInformationFragment myFragment = new CityInformationFragment();
        myFragment.setArguments(args);

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.fragment_main);
        ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager);
        FragmentPagerAdapter adapterViewPager;
        adapterViewPager = new CityDetailsPagerAdapter(getSupportFragmentManager());
        vpPager.setAdapter(adapterViewPager);
        // Give the TabLayout the ViewPager
        TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
        tabLayout.setupWithViewPager(vpPager);
    }
}

我的自定义Parcelable类如下:

    public class CityDetails implements Parcelable {
    private String mInformation;
    private String mNearPlace;
    private String mLocalFood;
    private int mItemImageResourceId;
    private int mNear_place_image_ResourceId;
    private int mLocal_food_image_ResourceId;

    public CityDetails(String information, String nearPlace, String localFood, int itemImageResourceId
            , int near_place_image_ResourceId, int local_food_image_ResourceId) {
        mInformation = information;
        mNearPlace = nearPlace;
        mLocalFood = localFood;
        mItemImageResourceId = itemImageResourceId;
        mNear_place_image_ResourceId = near_place_image_ResourceId;
        mLocal_food_image_ResourceId = local_food_image_ResourceId;

    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeString(mInformation);
        out.writeString(mNearPlace);
        out.writeString(mLocalFood);
        out.writeInt(mItemImageResourceId);
        out.writeInt(mNear_place_image_ResourceId);
        out.writeInt(mLocal_food_image_ResourceId);

    }

    private CityDetails(Parcel in) {
        mItemImageResourceId = in.readInt();
        mNear_place_image_ResourceId = in.readInt();
        mLocal_food_image_ResourceId = in.readInt();
        mNearPlace = in.readString();
        mLocalFood = in.readString();
        mInformation = in.readString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    // After implementing the `Parcelable` interface, we need to create the
    // `Parcelable.Creator<MyParcelable> CREATOR` constant for our class;
    // Notice how it has our class specified as its type.
    public static final Parcelable.Creator<CityDetails> CREATOR
            = new Parcelable.Creator<CityDetails>() {

        // This simply calls our new constructor (typically private) and
        // passes along the unmarshalled `Parcel`, and then returns the new object!
        @Override
        public CityDetails createFromParcel(Parcel in) {
            return new CityDetails(in);
        }

        // We just need to copy this and change the type to match our class.
        @Override
        public CityDetails[] newArray(int size) {
            return new CityDetails[size];
        }
    };

    public String getInformation() {
        return mInformation;
    }

    public String getNearPlace() {
        return mNearPlace;
    }

    public String getLocalFood() {
        return mLocalFood;
    }

    public int getItemImageResourceId() {
        return mItemImageResourceId;
    }

    public int getNear_place_image_ResourceId() {
        return mNear_place_image_ResourceId;
    }

    public int getLocal_food_image_ResourceId() {
        return mLocal_food_image_ResourceId;
    }
}

最后我的片段是:

    public class CityInformationFragment extends Fragment {

    public CityInformationFragment() {
        // Required empty public constructor
    }

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

        Bundle bundle = getArguments();

        if ((bundle != null)) {
            ArrayList<CityDetails> city = getArguments().getParcelable("key");

            // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
            // adapter knows how to create list items for each item in the list.
            CityDetailsAdapter adapter = new CityDetailsAdapter(getActivity(), city);

            // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
            // There should be a {@link ListView} with the view ID called list, which is declared in the
            // word_list.xml layout file.
            ListView listView = (ListView) rootView.findViewById(R.id.city_list);

            // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
            // {@link ListView} will display list items for each {@link Word} in the list.
            listView.setAdapter(adapter);
        }

        return rootView;
    }

}

修改 这是我的FragmentPagerDapter

public class CityDetailsPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 1;
private String tabTitles[] = new String[] { "City Information"};
private Context context;

public CityDetailsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    return new CityInformationFragment();
}
@Override
public CharSequence getPageTitle(int position) {
    // Generate title based on item position
    return tabTitles[position];
}


@Override
public int getCount() {
    return PAGE_COUNT;
}

} 而且,这是我的CityDetailsAdapter:

public class CityDetailsAdapter extends ArrayAdapter<CityDetails> {
// View lookup cache
private static class ViewHolder {
    TextView cityInformation;
    ImageView city_item_image;
    TextView city_near_place_Information;
    ImageView city_item_near_place_image;
    TextView city_local_food_Information;
    ImageView city_item_local_food_image;

}

public CityDetailsAdapter(Context context, ArrayList<CityDetails> cityDetails) {
    super(context, 0, cityDetails);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    CityDetails currentCity = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder; // view lookup cache stored in tag
    if (convertView == null) {
        // If there's no view to re-use, inflate a brand new view for row
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.city_list_item, parent, false);
                    /*Find the TextView and ImageView and set them on the VIewHolder*/
        viewHolder.cityInformation = (TextView) convertView.findViewById(R.id.cityInformation);
        viewHolder.city_near_place_Information = (TextView) convertView.findViewById(R.id.city_near_place_Information);
        viewHolder.city_local_food_Information = (TextView) convertView.findViewById(R.id.city_local_food_Information);
        viewHolder.city_item_image = (ImageView) convertView.findViewById(R.id.city_item_image);
        viewHolder.city_item_near_place_image = (ImageView) convertView.findViewById(R.id.city_item_near_place_image);
        viewHolder.city_item_local_food_image = (ImageView) convertView.findViewById(R.id.city_item_local_food_image);

        // Cache the viewHolder object inside the fresh view
        convertView.setTag(viewHolder);
    } else {
        // View is being recycled, retrieve the viewHolder object from tag
        viewHolder = (ViewHolder) convertView.getTag();
    }
    // Populate the data into the template view using the data object
    viewHolder.cityInformation.setText(currentCity.getInformation());
    viewHolder.city_item_image.setImageResource(currentCity.getItemImageResourceId());
    viewHolder.city_near_place_Information.setText(currentCity.getNearPlace());
    viewHolder.city_local_food_Information.setText(currentCity.getLocalFood());
    viewHolder.city_item_near_place_image.setImageResource(currentCity.getNear_place_image_ResourceId());
    viewHolder.city_item_local_food_image.setImageResource(currentCity.getLocal_food_image_ResourceId());

    return convertView;
}

尽可能具体地说明我正在犯的错误。因为作为初学者我可能无法理解一些概念,但我会尝试。无论如何,这是我的第一个问题,所以也许我没有说出正确的问题。 我们非常欢迎您的帮助。 感谢。

2 个答案:

答案 0 :(得分:1)

问题似乎与您的FragmentPagerAdapter类有关。您没有通过getItem()方法返回的片段传递任何参数,它只返回片段对象而不传递任何参数。

所以将方法修改为以下

@Override
public Fragment getItem(int position) {
    CityInfomationFragment fragment = new CityInformationFragment();
    Bundle args = new Bundle();
    args.putParcelableArrayList("key", your_array_list);
    fragment.setArguments(args);
    return fragment;
}

将array_list传递给活动中FragmentPagerAdapter类的构造函数,并使用字段变量保存它并在getItem()方法中引用它

修改

您在活动中创建的片段实例完全没有任何效果,除非您使用它执行任何片段事务。您可以将该片段实例传递给FragmentPagerAdater并从getItem()方法返回该实例,以在ViewPager中显示该片段。

答案 1 :(得分:0)

尝试以下行

ArrayList<CityDetails> city = getArguments().getParcelableArrayList("key");