Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use RecyclerView to realize Voting system in Android

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use RecyclerView to implement the voting system in Android. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Create a fragment_vote_list.xml to display the main page of the vote

(1) the title bar can be slid using Toolbar (2) the voting area can be slid, which is realized by RecyclerView.

Note: the font size of the interface and the width of the control can be adjusted by yourself. To use RecyclerView, you first need to add the corresponding dependent library to the build.gradle of the project. Add: implementation 'com.android.support:recyclerview-v7:24.2.1'

Create an item_vote.xml to display the specific content of the vote

(1) the main layout is implemented using LinearLayout, in which a TextView is added to display the voting questions, and CheckBox is used as the multi-check box for voting. (2) load the current Item into the main page of the vote

Third, create a voting information entity class as the adaptation type of the adapter and create a new VoteInfo.java class.

Public class VoteInfo {private String questionItem; private String [] answerItems; public VoteInfo (String questionItem,String [] answerItems) {this.questionItem=questionItem; this.answerItems=answerItems;} public String getQuestionItem () {return questionItem;} public String [] getAnswerItems () {return answerItems;}}

Next, you need to prepare an adapter for RecyclerView, create a new VoteInfoAdapter.java, let the adapter inherit from RecyclerView.Adapter, and specify the generics as VoteInfoAdapter.ViewHolder. Where ViewHolder is an inner class that we defined in VoteInfoAdapter.

Public class VoteInfoAdapter extends RecyclerView.Adapter {private List mVoteInfoList; @ Override public ViewHolder onCreateViewHolder (@ NonNull ViewGroup parent, int viewType) {View view= LayoutInflater.from (parent.getContext ()) .accepate (R.layout.itemtemtemvotegery parentMagee false); ViewHolder holder=new ViewHolder (view); return holder;} @ Override public void onBindViewHolder (@ NonNull ViewHolder holder, int position) {VoteInfo voteInfo=mVoteInfoList.get (position); holder.questionItem.setText (voteInfo.getQuestionItem ()); holder.answerItem_1.setText (voteInfo.getAnswerItems () [0]) Holder.answerItem_2.setText (voteInfo.getAnswerItems () [1]); holder.answerItem_3.setText (voteInfo.getAnswerItems () [2]);} @ Override public int getItemCount () {return mVoteInfoList.size ();} static class ViewHolder extends RecyclerView.ViewHolder {TextView questionItem; CheckBox answerItem_1; CheckBox answerItem_2; CheckBox answerItem_3; public ViewHolder (View itemView) {super (itemView); questionItem= (TextView) itemView.findViewById (R.id.item_vote_question_tv) AnswerItem_1= (CheckBox) itemView.findViewById (R.id.item_vote_answer1_cb); answerItem_2= (CheckBox) itemView.findViewById (R.id.item_vote_answer2_cb); answerItem_3= (CheckBox) itemView.findViewById (R.id.item_vote_answer3_cb);} public VoteInfoAdapter (List voteInfoList) {mVoteInfoList=voteInfoList;}}

Fifth, the adapter is ready to use RecyclerView and create a new ShowVoteAdapter.java class.

Public class ShowVoteActivity extends BaseActivity {@ BindView (R.id.vote_list_recycleview) RecyclerView recyclerView; private List voteInfoList=new ArrayList (); @ Override protected void onCreate (Bundle saveInstanceState) {super.onCreate (saveInstanceState); ScreenUtils.setContentViewWithOrientation (this, ScreenUtils.isPhone ()? R.layout.fragment_vote_list: R.layout.fragment_vote_list); initVoteInfo (); LinearLayoutManager linearLayoutManager=new LinearLayoutManager (this); recyclerView.setLayoutManager (linearLayoutManager); VoteInfoAdapter voteInfoAdapter=new VoteInfoAdapter (voteInfoList); recyclerView.setAdapter (voteInfoAdapter);} private void initVoteInfo () {VoteInfo vote1=new VoteInfo ("1. Which of the following is the best answer? new String [] {"AAAAAA", "BBBBBB", "CCCCCC"}); voteInfoList.add (vote1); VoteInfo vote2=new VoteInfo ("2. Which of the following is the best answer? new String [] {"AAAAAA", "BBBBBB", "CCCCCC"}); voteInfoList.add (vote2); VoteInfo vote3=new VoteInfo ("3. Which of the following is the best answer? new String [] {"AAAAAA", "BBBBBB", "CCCCCC"}); voteInfoList.add (vote3); VoteInfo vote4=new VoteInfo ("4. Which of the following is the best answer? new String [] {"AAAAAA", "BBBBBB", "CCCCCC"}); voteInfoList.add (vote4); VoteInfo vote5=new VoteInfo ("5. Which of the following is the best answer? new String [] {"AAAAAA", "BBBBBB", "CCCCCC"}); voteInfoList.add (vote5); VoteInfo vote6=new VoteInfo ("6. Which of the following is the best answer? new String [] {"AAAAAA", "BBBBBB", "CCCCCC"}); voteInfoList.add (vote6); VoteInfo vote7=new VoteInfo ("7. Which of the following is the best answer? new String [] {"AAAAAA", "BBBBBB", "CCCCCC"}); voteInfoList.add (vote7); VoteInfo vote8=new VoteInfo ("8. Which of the following is the best answer? new String [] {"AAAAAA", "BBBBBB", "CCCCCC"}); voteInfoList.add (vote8); VoteInfo vote9=new VoteInfo ("9. Which of the following is the best answer? new String [] {"AAAAAA", "BBBBBB", "CCCCCC"}); voteInfoList.add (vote9); VoteInfo vote10=new VoteInfo ("10. Which of the following is the best answer? new String [] {"AAAAAA", "BBBBBB", "CCCCCC"}); voteInfoList.add (vote10); VoteInfo vote11=new VoteInfo ("11. Which of the following is the best answer? new String [] {"AAAAAA", "BBBBBB", "CCCCCC"}); voteInfoList.add (vote11);}}

6. You need to register ShowVoteActivity in AndroidManifest.xml in order to start normally.

Thank you for reading! This is the end of the article on "how to solve the voting system using RecyclerView in Android". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report