Android Learning Note (18): ListView and Ratingbar

Source: Internet
Author: User

In the study notes (17), we have further explored the ListView, however, given an example of a list of elements can have multiple widgets, and can flexibly set their values, but these widgets lack of interaction, and GetView () call, To entry the list, we want to be able to trigger the change in the entry.

This time, we continue to study according to "Beginging Android 2", combined with Ratingbar, the program is a little more complicated. Ratingbar look at the peer for the Media library, we used Ratingbar to replace the previous example of the icon, when the Ratingbar is set to Samsung, the text after the entry is changed to uppercase, if lower than Samsung will revert to the original lowercase display.

Example: custom data structures and triggering processing of internal widgets

1) Android XML file: Replace the previous example with Ratingbar ImageView

<?xml version= "1.0" encoding= "Utf-8",
<LinearLayout  ...;
  < Ratingbar  android:id= "@+id/c85_rating"
    android:layout_width= "wrap_content"
     android:layout_height= "Wrap_content"
    android: numstars  = "3"  <!--set up Samsung peer mode-->
    android: stepsize  = "0.5"    <!--step 0.5, which is to allow 2.5 star rating-->
    android: rating  = "2"/>   <!--default is 2 star-->
  <textview android:id= "@+id/c85_label"
    android: paddingleft= "2px"
    android:paddingright= "2px"
    android:paddingtop= " 10px "
    android:textsize=" 24sp "
    android:layout_width=" Fill_parent "
    android:layout_height= "Wrap_content"/>&NBSP;&NBSP;
</linearlayout>

2) set up custom data structures to store information and provide methods for querying information

In the previous example, we used arraylist<string> to store the data information for each cell, in this case, as a more general way, each cell information is our custom class Rowmodel.

Class rowmodel{
String label; Stores the current text of the entry display, given by calling ToString (), if Samsung will provide an uppercase display.
float rating = 2.0f; Storage of Entry star data, corresponding to Ratingbar star display

Rowmodel (String label) {
This.label = label;
}
Public String toString () {
if (rating >= 3.0) {
return Label.touppercase ();
}
return label;
}
}

In our main class, we set our data information list according to a custom structure and import it into the list adapter, and we add a method to get the data of the unit from the data information according to position (index).

protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
arraylist<rowmodel> list = new arraylist<rowmodel> (); Step 1:list as the storage of data
for (String s:items) {///Step 2: Import string[] Items into list, this is a special way of writing, I will generally be honest for (int i =0; I <items.length; i++).
List.add (new Rowmodel (s));
}
Setlistadapter (new Ratingadapter (list)); Step 3: Set up a custom ListAdapter (specifically processed later) and import the information data list into it
}
Depending on the location of the list, the specific list element is obtained, and the general add,del,find processing is equivalent to the find
Private Rowmodel Getmodel (int position) {
Return ((Ratingadapter) Getlistadapter ()). GetItem (position);
}

3) View and widget information bundle for the list unit for fast positioning widgets

Based on previous studies, in order to make the program run more efficiently, we use Settag to bundle the view of the list unit's UI and the class of the widget information in the storage unit UI so that the widget can be quickly positioned.

Step 1: Set up the related classes that store the widgets in the list cell view.

In fact, we can put these widget information together with the data information in 2, in this example, the program will learn more, but this is not good, we try to put the UI-related information and data information together, or UI modification or size adaptation of the problem.

Private Class viewwrapper{
View Base;
Ratingbar rate = null;
TextView label = NULL;

Viewwrapper (View base) {
This.base = base;
}

Ratingbar Getratingbar () {
if (rate = = null)
Rate = (Ratingbar) Base.findviewbyid (r.id.c85_rating);
return rate;
}

TextView Getlabel () {
if (label = = NULL)
Label = (TextView) Base.findviewbyid (R.id.c85_label);
return label;
}
}

Step 2:list The rendering of the Unit view (GetView) and provide the processing that the widget triggers

A list unit of view corresponds to two content, one is stored data, can be obtained through Getmodel, the other is the corresponding unit UI widget formation storage, through Gettag () and Settag (), this has been learned in the last learning, We also need to increase the trigger of the widget in view, in this case, when the star of Ratingbar is changed, it may be necessary to rewrite the display of the following post. We look at the code specifically:

Private class Ratingadapter extends arrayadapter<rowmodel>{
Step 2.1: Set the constructor, put the data information into Arrayadapter, so that the data information can be obtained through GetItem (), and also set the layout format
Ratingadapter (arraylist<rowmodel> list) {
Super (Chapter8test5.this,r.layout.entry,list);
}

Step 2.2: Write the rendering of each cell in the ListView
Public ViewGetView(int position, View Convertview, ViewGroup parent) {
View row = Convertview;
Viewwrapper wrapper;
Ratingbar ratebar = null;
Step 2.3: If no view is created, create it according to layout and bundle the object of the widget's storage class with it as tag
if (row = = null) {
Layoutinflater Inflater=getlayoutinflater ();
row = Inflater.inflate (R.layout.entry, Parent,false);
wrapper = new Viewwrapper (row);
Row.settag (wrapper);
Step 2.4: When generating the view, add the trigger processing of the widget
Ratebar = Wrapper.getratingbar ();
Ratebar.Setonratingbarchangelistener(New Ratingbar.Onratingbarchangelistener() {
public voidonratingchanged(Ratingbar Ratingbar, float rating, Boolean fromuser) {
Step 2.4.1: Store changed data
Integer index = (integer) ratingbar.gettag ();
Rowmodel model = Getmodel (index);
Model.rating = rating;
Step 2.4.2: Setting changes
LinearLayout parent = (linearlayout) ratingbar.getparent ();
TextView label = (TextView) Parent.findviewbyid (R.id.c85_label);
Label.settext (Model.tostring ());
}
});
}else{//Step 2.4: Use the existing view to obtain the appropriate widget
Wrapper = (Viewwrapper) row.gettag ();
Ratebar = Wrapper.getratingbar ();
}
Step 2.5: Set the contents of the display, and set the Ratingbar bundle tag to the location of list, because Settag () is the view method, so we can not drop to add in viewwrapper, so we need to load the widget in Viewwrapper , where the Ratebar is selected for bundling.
Rowmodel model= Getmodel (position);
Wrapper.getlabel (). SetText (Model.tostring ());
Ratebar.Settag(New Integer (position));
Ratebar.setrating (model.rating);
return row;
}
}

In this example we have an experiment to see when convertview can be null, a screen can display 0-8 row, these list elements are null, need to be created by the program, but when I mixed the screen, I imagined, The following elements should also be 0 for the first time, but to my surprise, only Position=14 's appearance row=null. For the case of the scroll screen, the next screen of Android may be processed based on the first screen's processing of the UI. As a result, Android's intelligent handling of the UI is not very well-handled, so any problem with data that is not purely a matter of initial assignment of UI problems, do not just place the initial processing in if (row==null), or it can cause unpredictable surprises . For example, we put the Ratebar.settag (new Integer (position)) in step 2.5 in the IF (row==null) to get an abnormal result, Because not all of the widgets in the list element are successfully bundled in the initial case, we place it outside or in the notification mode in the IF and else judgments, ensuring that all cases are overwritten.

Listadapter:cursoradapter

Generally speaking, we can use Arrayadapter to apply a lot of cases, there are other adapter, using similar, but cursoradapter some different, through Newview () and BindView (), if not created, Use Newview (), and then call BindView (), if it is already created, use BindView ().

RELATED links: My Andriod development related articles

Android Learning Note (18): ListView and Ratingbar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.