Android listview builds voting items that support single and multiple choices

introduction

In android app development, we sometimes encounter the need to provide an option list for users to choose. For example, in voting projects, we provide some topics for users to choose. Each topic has several options. Users choose the options of these topics and submit them later.

Taking a single and multiple-choice voting project as an example, this paper demonstrates how to build in a listview Check@R_ 984_ 2419 @ list and Ra@R_ 403_ 2369@Button List, and analyzes the principle and idea of implementation, so as to provide friends in need with reference.

The demonstration effect of the project is as follows.

data source

Usually our data source comes from the database. First, we build the voting item class subjectitem.

Then we construct a list collection of subjectitem objects as the data source of our voting project. In the actual project, the data source can come from the database voting project table.

How to display voting items in a list on a listview control

First, let's understand the process of displaying list data in the listview control. 1. Define a layout showing each line of the list. The file name of the layout we define here is listview_ subject_ item.xml。 2. Define the layout for displaying listview. The file name of the layout we define here is listview_ subject_ activity.xml。 3. Define the data adapter subjectadapter for listview.

Defines the layout of each column of the listview control

listview_ subject_ The layout of each column in the listview control defined by the item.xml file. Our voting items here support single choice and multiple items. You can include the layout of each column view Check@R_ 984_ 2419 @ and Ra@R_ 403_ 2369@Button Control to automatically display (hide) the corresponding control according to the voting type (single or multiple choice) of the current item when the view is displayed on the mobile phone interface.

Define the layout of the display listview

Define the layout Layou for displaying listview. The file name is listview_ subject_ Activity.xml uses the relativelayout layout here. The submit button is fixed at the bottom of the screen to facilitate users to submit voting information.

Define data adapter subjectadapter

listview_ subject_ The activity.xml file defines an activity named Lv_ The listview of the subject. How does this listview match the layout of each column view in the listview control_ subject_ Item.xml, and we have defined the data source of the voting subject item and how it is associated with listview. To complete these, we must rely on an apdater adapter class. The listview control is associated with the adapter through the setadapter method. In the adapter, use the getview method and the layout listview of the column view_ subject_ Item.xml. The data source is passed to the adapter through the parameters of the adapter's custom constructor.

Defines the activity component that displays voting items

Finally, we define an activity component to display the voting items. In order to cancel the previously selected items of the same topic after the single item is selected, a map is defined( ra@R_403_2369 @Buttonselectedmaps) to store the selected item information of the radio topic. Key is the radio topic ID and value is the selected item ID. In this way, when the user selects a radio item, the program first sets the status of the previously selected item in the subjectitemmap project subject in the subjectadapter object to false, then sets the currently selected item to true, and then updates the listview to achieve the radio selection effect.

Get voting results

Code download: https://github.com/zoupeiyang/ListViewCheckBox

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>