I have an activity which parses the json & sends its values to viewPager
public class SwipeQuestions extends AppCompatActivity {
// Declare Variables
ViewPager viewPager;
PagerAdapter adapter;
ArrayList<String> questions = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe_questions);
viewPager = findViewById(R.id.pager);
viewPager.setOffscreenPageLimit(10);
adapter = new ViewPagerAdapter(SwipeQuestions.this, questions);
viewPager.setAdapter(adapter);
}
}
& its XML(activity_swipe_questions) is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
each question should appear in separate view with a editText where the user can enter the text & on the last question there will be a submit button which will save all the editText values in to an array or arrayList. The Viewpager Adapter class is extending PagerAdapter is defined below
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
private Context context;
LayoutInflater inflater;
Button submitButton;
private ArrayList<String> enteredAnswerArray = new ArrayList<String>();
private ArrayList<String> questions;
public ViewPagerAdapter(Context context, ArrayList<String> questions) {
this.context = context;
this.questions = questions;
}
@Override
public int getCount() {
return questions.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == (object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
TextView txtQuestion;
EditText enteredAnswer;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item, container,false);
txtQuestion = itemView.findViewById(R.id.question); // Locate the TextViews in viewpager_item.xml
txtQuestion.setText(questions.get(position)); // Capture position and set to the TextViews
submitButton = itemView.findViewById(R.id.submit_btn);
enteredAnswer = itemView.findViewById(R.id.answer_textbox);
if(position == (questions.size()-1)){
submitButton.setVisibility(View.VISIBLE);
}
else {
submitButton.setVisibility(View.GONE);
}
submitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
System.out.println(enteredAnswerArray.size());
// System.out.println("0"+enteredAnswerArray.get(0));
// System.out.println("1"+enteredAnswerArray.get(1));
Log.i("clicked", "button izz clicked");
}
});
(container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
(container).removeView((RelativeLayout) object); // Remove viewpager_item.xml from ViewPager
}
}
& its XML(viewpager_item) is defined below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="10dp" >
<TextView
android:id="@+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/answer_textbox"
android:text="Question" />
<EditText
android:id="@+id/answer_textbox"
style="@android:style/Widget.DeviceDefault.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/question"
android:ems="10"
android:fontFamily="casual"
android:inputType="textMultiLine|textImeMultiLine"
android:singleLine="false" />
<Button
android:id="@+id/submit_btn"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="@+id/answer_textbox"
android:text="Submit"/>
</RelativeLayout>
Now for every question the user has entered some text in the editText fields & i want to save each editText value to an array or arrayList on click of submit button which is on the last view of viewPager.
Rite now i've 10 questions so i set viewPager.setOffscreenPageLimit() to 10 so that the values entered by the user doesn't get deleted as user slides the screen
Can anyone help me out how the save the editText values in a viewPager. I googled a lot But couldn't find anything relevant
The easiest solution is to:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.