gpt4 book ai didi

java - 动态 fragment 的问题

转载 作者:行者123 更新时间:2023-12-01 18:10:50 26 4
gpt4 key购买 nike

好吧,我问了一个问题,得到了大约 90% 的答案,我需要解决剩下的 10%。

它基本上是一个事件页面,因此在单击 StackView 中的所选项目时,它会打开另一个 fragment ,其中有 2 个 textView 和 1 个 imageView ,所有这些页面(10+)都是对称的,因此我决定将其设为动态,以便于操作和处理。

事件 fragment

   public class EventsFragment extends android.support.v4.app.Fragment {
private StackView stackView;
private ArrayList<Stack_Items> list;
TypedArray eventLogo;
String eventName[],eventDesc[];

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

eventLogo = getResources().obtainTypedArray(R.array.event_stack_icon);
eventName = getResources().getStringArray(R.array.event_stack);
eventDesc = getResources().getStringArray(R.array.event_desc_stack);
View view = inflater.inflate(R.layout.events_layout, null);
stackView = (StackView) view.findViewById(R.id.stackView1);
list = new ArrayList<Stack_Items>();

//Adding items to the list
for (int i = 0; i < eventLogo.length(); i++) {
list.add(new Stack_Items(eventName[i], eventLogo.getResourceId(i, -1)));
}
//Calling adapter and setting it over stackView
Stack_Adapter adapter = new Stack_Adapter(getActivity().getApplicationContext(), list);
stackView.setAdapter(adapter);
stackView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View
view, int position, long id) {
MainActivity mainActivity = (MainActivity) getActivity();
FragmentTransaction fragmentTransaction = mainActivity.getSupportFragmentManager().beginTransaction();
Fragment eventDescFragment = new DynamicEventsPage().newInstance(R.array.event_stack,R.array.event_desc_stack,eventLogo.getResourceId(position, -1));
fragmentTransaction.replace(R.id.containerView, eventDescFragment);
fragmentTransaction.commit();
}
});
adapter.notifyDataSetChanged();
return view;
}
}

动态事件页面

public class DynamicEventsPage extends Fragment{
public static final String BUNDLE_STRING_KEY = "BUNDLE_STRING_KEY";
public static final String BUNDLE_DRAWABLE_KEY = "BUNDLE_DRAWABLE_KEY";
public static final String BUNDLE_STRING_DESC_KEY = "BUNDLE_STRING_DESC_KEY";

public static Fragment newInstance(final int stringId,final int descStringId, final int drawableId) {
Bundle bundle = new Bundle();
bundle.putInt(BUNDLE_STRING_KEY, stringId);
bundle.putInt(BUNDLE_DRAWABLE_KEY, drawableId);
bundle.putInt(BUNDLE_STRING_DESC_KEY,descStringId);
Fragment fragment = new DynamicEventsPage();
fragment.setArguments(bundle);
return fragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.dynamic_events_page, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Bundle bundle = getArguments();
((ImageView)view.findViewById(R.id.imageView)).setImageResource(bundle.getInt(BUNDLE_DRAWABLE_KEY));
((TextView)view.findViewById(R.id.textView)).setText(bundle.getInt(BUNDLE_STRING_KEY));
((TextView)view.findViewById(R.id.textViewdesc)).setText(bundle.getInt(BUNDLE_STRING_DESC_KEY));
}

}

dynamic_events_page.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView"
android:layout_weight="0.40" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textViewdesc"
android:layout_weight="0.40" />

<ImageView
android:layout_width="228dp"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_weight="0.40" />

</LinearLayout>

获取错误的是

E/AndroidRuntime: android.content.res.Resources$NotFoundException: String resource ID #0x7f0b0001

E/AndroidRuntime: at package.DynamicEventsPage.onViewCreated(DynamicEventsPage.java:39)

那么解决办法是什么?

最佳答案

更改 newInstance 以采用 String 而不是 int

public static Fragment newInstance(final String eventName,final String eventDescr, final int drawableId) {
Bundle bundle = new Bundle();
bundle.putString(BUNDLE_STRING_KEY, stringId);
bundle.putInt(BUNDLE_DRAWABLE_KEY, drawableId);
bundle.putString(BUNDLE_STRING_DESC_KEY,descStringId);
Fragment fragment = new DynamicEventsPage();
fragment.setArguments(bundle);
return fragment;
}

和 onViewCreate 读取 String 而不是 int

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Bundle bundle = getArguments();
((ImageView)view.findViewById(R.id.imageView)).setImageResource(bundle.getInt(BUNDLE_DRAWABLE_KEY));
((TextView)view.findViewById(R.id.textView)).setText(bundle.getString(BUNDLE_STRING_KEY));
((TextView)view.findViewById(R.id.textViewdesc)).setText(bundle.getString(BUNDLE_STRING_DESC_KEY));
}

在 onItemClick 中更改

Fragment eventDescFragment = new DynamicEventsPage().newInstance(R.array.event_stack,R.array.event_desc_stack,eventLogo.getResourceId(position, -1));

String name = (position < eventName.length) ? eventName[position] : "";
String description = (position < eventDesc.length) ? eventDesc[position] : "";
Fragment eventDescFragment = DynamicEventsPage.newInstance(eventName, description,eventLogo.getResourceId(position, -1));

关于java - 动态 fragment 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33119712/

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