My 2D array has 5x3. In array I have stored id, name, image source string. I trying to put these values in Adapter to make newInstance, but all values in Fragment are passed as null
.
My Adapter:
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;
}
}
My Fragment:
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;
}
}
Have no time to answer, but i managed it myself.
Removing while (i < 5)
statement from ScreenSlidePagerAdapter
and leave all code in it outside statement, and all working.