嗯,我问了一个问题,得到了90%的答案,我需要解决剩余的10%。动态片段问题
这基本上是一个活动页面,以便在点击了从StackView选择的项目,它打开其中有2个textViews和1个ImageView的, 所有这些页面(10+)是对称的,所以我决定到另一个片段使其变得动态,这样就很容易处理和处理。
EventsFragment
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;
}
}
DynamicEventPage
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)
那么是什么修复?
什么是DynamicEventsPage.java:39? –