Source code analysis of Jamendo open-source online music player (III)

Source: Internet
Author: User

3. AsyncTask
New newstask(cmd.exe cute (Void) null); execute an asynchronous task without passing any parameters. Asynchronously load the information of a recording and call the corresponding API. The following describes the API. After Successful loading, the gallery of the recording set is displayed. Each item in the gallery is a custom ImageView, this ImageView can asynchronously load the album image and cache it according to the url, that is, com. teleca. jamendo. widget. remoteImageView, which will be detailed later. If loading fails, retry is displayed, and the toast prompt is displayed.
4. adapter
FillHomeListView () in the onResume () method. This is the method used to fill the listview. We can see that there are two different la s in the listview, one of which is similar to the delimiter layout, one is clickable menu items. First, analyze the adapter of the menu items that can be clicked.
The com. teleca. jamendo. adapter package is an adapter-related class.
Let's take a look at ArrayListAdapter first. java, which is inherited from BaseAdapter. The dataset in this adapter is placed in the ArrayList <T> mList. The subclass inherits it and overwrites its own getView () method. The program can use the adapter. setList (ArrayList <T> list) or adapter. setList (T [] list) fills in values of specific types.
The menu items that can be clicked are a subclass of ArrayListAdapter: PurpleAdapter, and the filling type is <PurpleEntry>. PurpleEntry contains the drawable and text of each row and the operations triggered after clicking.
The following is the overall adapter of the entire list that includes the separator: SeparatedListAdapter.
[Java]
SeparatedListAdapter separatedAdapter = new SeparatedListAdapter (this );
SeparatedAdapter. addSection (getString (R. string. browse_jamendo), mBrowseJamendoPurpleAdapter );
SeparatedAdapter. addSection (getString (R. string. my_library), mMyLibraryPurpleAdapter );
We enter SeparatedListAdapter to check the constructor. First, an ArrayAdapter <String> headers is initialized. This is the adapter of the separator. addSection (String section, Adapter adapter) is the value filled with headers, at the same time, put the two purpleadapters above into the map, and then look at the getCount () method, you can see the total number of items in the listview, that is, in addition to the number of the two purpleadapters above, but also add two separators. Next, let's take a look at the getView () method. When filling in, we mainly call the getView () method of headers and the getView () method of each PurpleAdapter, which is mainly logic. It should be okay to go through the process carefully.
The following describes three overwriting methods:
[Java]
Public int getViewTypeCount ()
Public int getItemViewType (int position)
Public boolean isEnabled (int position)
It doesn't matter if you comment out the three methods, and the program can run the same way. The first two methods are mainly used to cache items in listview and analyze the source code, in the setAdapter () method of listview, there is an mRecycler variable. You can see its annotations. This variable is used to cache the view and avoid re-create at next layout. MRecycler is an internal class of the parent class AbsListView of ListView. Next, mRecycler. setViewTypeCount (mAdapter. getViewTypeCount (); The getViewTypeCount () method is called here. In order to initialize this ArrayList for cache view, when listview fills the item, mRecycler is called. getScrapView (position): This method calls the getItemViewType (int position) method, obtains the cached view based on the returned type, and passes it to convertView in the getView () method of the adapter, this allows reuse.
Return to the program. The getView () in this adapter does not use the cache view to determine whether convertView is empty. Instead, each view is directly generated, so here the two methods are overwritten mainly for the service isEnabled (int position). This method is used to determine whether this row can be clicked. The headers used for separation cannot be clicked. Of course, if you comment out the three methods, the headers will be able to click and there will be no errors. Return to the fillHomeListView () of HomeActivity, and finally mHomeListView. setOnItemClickListener (mHomeItemClickListener); sets the click event of listview. If you click the separator headers, the program will try this exception, so it's okay. If you click it normally, the method in the previously set listener will be called.
Next, we will analyze the music API.

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.