Android ApiDemos example resolution (171): Views-& gt; Lists-& gt; 4. Lis

Source: Internet
Author: User

Previous: http://www.bkjia.com/kf/201209/156201.html
In this example, a custom View-SpeechView is returned. This View is composed of two textviews. One TextView displays the Title and one View displays the content. Its constructor is defined as follows:


[Java]
Public SpeechView (Context context, String title, String words ){
Super (context );
 
This. setOrientation (VERTICAL );
 
// Here we build the child views in code. They cocould also have
// Been specified in an XML file.
 
MTitle = new TextView (context );
MTitle. setText (title );
AddView (mTitle, new LinearLayout. LayoutParams (
LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT ));
 
MDialogue = new TextView (context );
MDialogue. setText (words );
AddView (mDialogue, new LinearLayout. LayoutParams (
LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT ));
}

Public SpeechView (Context context, String title, String words ){
Super (context );

This. setOrientation (VERTICAL );

// Here we build the child views in code. They cocould also have
// Been specified in an XML file.

MTitle = new TextView (context );
MTitle. setText (title );
AddView (mTitle, new LinearLayout. LayoutParams (
LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT ));

MDialogue = new TextView (context );
MDialogue. setText (words );
AddView (mDialogue, new LinearLayout. LayoutParams (
LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT ));
}

The getView of the custom SpeechListAdapter is defined as follows:

 

[Java]
Public View getView (int position, View convertView,
ViewGroup parent ){
SpeechView sv;
If (convertView = null ){
Sv = new SpeechView (mContext, mTitles [position],
MDialogue [position]);
} Else {
Sv = (SpeechView) convertView;
Sv. setTitle (mTitles [position]);
Sv. setDialogue (mDialogue [position]);
}
 
Return sv;
}

Public View getView (int position, View convertView,
ViewGroup parent ){
SpeechView sv;
If (convertView = null ){
Sv = new SpeechView (mContext, mTitles [position],
MDialogue [position]);
} Else {
Sv = (SpeechView) convertView;
Sv. setTitle (mTitles [position]);
Sv. setDialogue (mDialogue [position]);
}

Return sv;
}

 

Therefore, each item in the list is displayed as a SpeechView:

 

 

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.