在 ViewPager2 中显示二维数组数据
Display 2D array data in ViewPager2
我的二维数组有 5x3。在数组中,我存储了 id、名称、图像源字符串。我试图将这些值放入 Adapter 中以生成 newInstance,但 Fragment 中的所有值都作为 null
传递。
我的适配器:
private class ScreenSlidePagerAdapter extends FragmentStateAdapter {
ScreenSlidePagerAdapter(FragmentActivity fa) {
super(fa);
}
@Override
public Fragment createFragment(int position) {
if (position < q_question.length) {
int i = 0;
int id = 0;
String name = "";
String src = "";
while (i < 5) {
if (!TextUtils.isEmpty(q_question[i][0]) && TextUtils.isDigitsOnly(q_question[i][0])) {
id = Integer.parseInt(q_question[i][0]);
}
name = q_question[i][1];
src = q_question[i][2];
i++;
}
return ScreenSlidePageFragment.newInstance2(id, name, src);
}else
return null;
}
@Override
public int getItemCount() {
return q_question.length;
}
}
我的片段:
public class ScreenSlidePageFragment extends Fragment {
private static final String ARG_RESOURCE_ID = "resource_id";
private static final String ARG_RESOURCE_NAME = "resource_name";
private static final String ARG_RESOURCE_SRC = "resource_src";
private int id;
private String Name;
private String Src;
public ScreenSlidePageFragment() {
// Required empty public constructor
}
public static Fragment newInstance2(int id, String name, String src) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_RESOURCE_ID, id);
args.putString(ARG_RESOURCE_NAME, name);
args.putString(ARG_RESOURCE_SRC, src);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
id = getArguments().getInt(ARG_RESOURCE_ID);
Name = getArguments().getString(ARG_RESOURCE_NAME);
Src = getArguments().getString(ARG_RESOURCE_SRC);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
// assign our image's resource id here
ImageView imageView = view.findViewById(R.id.question_image);
Button button = view.findViewById(R.id.answer_one);
imageView.setImageDrawable(Drawable.createFromPath(Src));
button.setText(Name);
Log.e("test", id + Src +"//"+ Name); // There id always are 3, and src and name null
return view;
}
}
没时间回答,我自己解决了。
从 ScreenSlidePagerAdapter
中删除 while (i < 5)
语句并将其中的所有代码保留在语句之外,并且一切正常。
我的二维数组有 5x3。在数组中,我存储了 id、名称、图像源字符串。我试图将这些值放入 Adapter 中以生成 newInstance,但 Fragment 中的所有值都作为 null
传递。
我的适配器:
private class ScreenSlidePagerAdapter extends FragmentStateAdapter {
ScreenSlidePagerAdapter(FragmentActivity fa) {
super(fa);
}
@Override
public Fragment createFragment(int position) {
if (position < q_question.length) {
int i = 0;
int id = 0;
String name = "";
String src = "";
while (i < 5) {
if (!TextUtils.isEmpty(q_question[i][0]) && TextUtils.isDigitsOnly(q_question[i][0])) {
id = Integer.parseInt(q_question[i][0]);
}
name = q_question[i][1];
src = q_question[i][2];
i++;
}
return ScreenSlidePageFragment.newInstance2(id, name, src);
}else
return null;
}
@Override
public int getItemCount() {
return q_question.length;
}
}
我的片段:
public class ScreenSlidePageFragment extends Fragment {
private static final String ARG_RESOURCE_ID = "resource_id";
private static final String ARG_RESOURCE_NAME = "resource_name";
private static final String ARG_RESOURCE_SRC = "resource_src";
private int id;
private String Name;
private String Src;
public ScreenSlidePageFragment() {
// Required empty public constructor
}
public static Fragment newInstance2(int id, String name, String src) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_RESOURCE_ID, id);
args.putString(ARG_RESOURCE_NAME, name);
args.putString(ARG_RESOURCE_SRC, src);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
id = getArguments().getInt(ARG_RESOURCE_ID);
Name = getArguments().getString(ARG_RESOURCE_NAME);
Src = getArguments().getString(ARG_RESOURCE_SRC);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
// assign our image's resource id here
ImageView imageView = view.findViewById(R.id.question_image);
Button button = view.findViewById(R.id.answer_one);
imageView.setImageDrawable(Drawable.createFromPath(Src));
button.setText(Name);
Log.e("test", id + Src +"//"+ Name); // There id always are 3, and src and name null
return view;
}
}
没时间回答,我自己解决了。
从 ScreenSlidePagerAdapter
中删除 while (i < 5)
语句并将其中的所有代码保留在语句之外,并且一切正常。