in Education by
How can I use Viewpager Adapter Button into MainActivity? I am developing a quiz app and I am comparing button value into MainActivity. How can I access ViewPagerAdapter class buttons In MainActivity? @Override protected void onStart() { super.onStart(); Ref.child("Exam_Category").child("PRACTICE TEST").child("PREVIOUS YEAR PAPER'S").child(ct1).child(ct2).child("ENGLISH COMPREHENSION").addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { if (dataSnapshot.exists()){ for (DataSnapshot data : dataSnapshot.getChildren()) { key = data.getKey().toString(); Ref.child("Exam_Category").child("PRACTICE TEST").child("PREVIOUS YEAR PAPER'S").child(ct1).child(ct2).child("ENGLISH COMPREHENSION").child(String.valueOf(key)) .addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { if (dataSnapshot.exists()) { final questions studentDetails = dataSnapshot.getValue(questions.class); list.add(studentDetails); adapter = new RecyclerViewAdapter(Previous_Yr_Quiz.this, list); viewPager.setAdapter(adapter); } } @Override public void onCancelled(@NonNull DatabaseError databaseError) { } }); } } //total_q.setText(Question_No +" / "+key); } @Override public void onCancelled(@NonNull DatabaseError databaseError) { } }); This is My ViewPager Adapter public class RecyclerViewAdapter extends PagerAdapter { // Declare Variables Context context; LayoutInflater inflater; public List list; public RecyclerViewAdapter(Context context, List TempList) { this.context = context; this.list = TempList; } @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == ((RelativeLayout) object); } @Override public Object instantiateItem(ViewGroup container, final int position) { // Declare Variables TextView question; final Button option1,option2,option3,option4; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View itemView = inflater.inflate(R.layout.quiz_layout_listview, container, false); // Locate the TextViews in viewpager_item.xml question = (TextView)itemView.findViewById(R.id.question); option1 = (Button) itemView.findViewById(R.id.optiona); option2 = (Button) itemView.findViewById(R.id.optionb); option3 = (Button) itemView.findViewById(R.id.optionc); option4 = (Button) itemView.findViewById(R.id.optiond); // Capture position and set to the TextViews questions studentDetails = list.get(position); question.setText(studentDetails.getQuestion()); option1.setText(studentDetails.getOption1()); option1.setId(position); option2.setText(studentDetails.getOption2()); option3.setText(studentDetails.getOption3()); option4.setText(studentDetails.getOption4()); option1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { option1.setBackgroundResource(R.drawable.wrong); } }); ((ViewPager) container).addView(itemView); return itemView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // Remove viewpager_item.xml from ViewPager ((ViewPager) container).removeView((RelativeLayout) object); } private int getItem(int i) { return list.size(); } } How can used button options A, B, C, D OnClick in MainActivity because I compare button value into A, B, C, D buttons.... JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You have two options: 1) Make Buttons as class members and access them from your Activity by get methods. 2) Write an Interface that will have the callback methods so you know which Button is being clicked. Once you write the interface you pass it as a constructor parameter or by a set method. Something like: interface QuizListener { void answered(Button whichButton); } ... [RecyclerViewAdapter.java] public RecyclerViewAdapter(Context context, List TempList, QuizListener quizListener) { this.context = context; this.list = TempList; this.quizListener = quizListener; } ... option1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (quizListener != null) { quizListener.answered(option1); } option1.setBackgroundResource(R.drawable.wrong); } });

Related questions

0 votes
    I have two buttons in MainActivity, the first one opens a custom DialogFragment with some spinners and in ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 11, 2022 in Education by JackTerrance
0 votes
    I want to make a button Go To Cart visible after adding a product into cart. But when page gets ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    In my layout I am trying to get the CirclePageIndicator to sit at the bottom of my image and for ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    I have some website which requires a logon and shows sensitive information. The person goes to the page, is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I've set up a simple app to run in a page tab. After authorizing the use of the app, just for ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 16, 2022 in Education by JackTerrance
0 votes
    State T' for true and F' for false statements: 1. Home button is used to display the home page. 2. ... be used to pay bills. Select the correct answer from above options...
asked Dec 10, 2021 in Education by JackTerrance
0 votes
    I have a ViewPager below an AppBarLayout (with a Toolbar and a TabLayout). I cannot understand why the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    a) Harshita wants to create an HTML form. Which tag should she use to create this form? Her teacher has asked ... plz answer this fast Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    Can we use multiple ScriptManager on a page?...
asked Dec 31, 2020 in Technology by JackTerrance
0 votes
    you use the ………… … button to look at the slides in different ways Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    Explain how you can use backbone.js for multiple page web app ?...
asked Nov 13, 2020 in Technology by JackTerrance
0 votes
    I'm trying to make the text move continuously by pressing a button but right now it only moves an ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I want to make a blackberry button with images, the one image should be shown when focused and another ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    What is the use of Track Page View in Google Analytics?...
asked Oct 3, 2020 in Technology by JackTerrance
0 votes
    I styled a button: .subscribe_button { background-color: red; } .subscribe_button:hover { background-color: ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
...