gpt4 book ai didi

android - Parcelable with arraylist 将值从 Activity 传递到 fragment android

转载 作者:行者123 更新时间:2023-11-30 01:00:23 25 4
gpt4 key购买 nike

我正在尝试将自定义 ArrayList 从 Activity 传递到 fragment ,但不明白代码有什么问题。没有错误,但即使是它们我也无法获得预期的输出。在努力修复了将近 3 天之后,我完全迷路了(是的,但我只是一个初学者)。请帮我解决它。目的是我想将该 fragment 用于其他 Activity 。

这是我的 Activity 类(class):

 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;
}
}

最后我的 fragment 是:

    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;
}

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

最佳答案

问题似乎出在您的 FragmentPagerAdapter 类上。您没有通过 getItem() 方法返回的 fragment 传递任何参数,它只是返回 fragment 对象而不向其传递任何参数。

所以修改方法如下

@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 从 Activity 传递给 FragmentPagerAdapter 类的构造函数并使用字段变量保存它并在 getItem() 方法中引用它

编辑

您在 Activity 中创建的 fragment 实例根本不起作用,除非您对其进行任何 fragment 事务处理。您可以将该 fragment 实例传递给 FragmentPagerAdater 并从 getItem() 方法返回相同的实例以在 ViewPager 中显示该 fragment 。

关于android - Parcelable with arraylist<CustomClass> 将值从 Activity 传递到 fragment android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39528018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com