Android ListView custom BaseAdapter and androidbaseadapter

Source: Internet
Author: User

Android ListView custom BaseAdapter and androidbaseadapter

First

 

It can be seen from the above: each item of the LISTVIEW in the page is composed of two textviews. during initialization, only the first TEXTVIEW is displayed. Here we become the title, the setVisible attribute of the content part is "GONE ". Add an onClick listener for Ite. The content is displayed each time you click the listener. You need to call notifyDataSetChanged () of BaseAdapter to refresh the listener.

 

Code Section:

 

1 class MyLayout extends LinearLayout {2 3 private TextView mTitle; 4 private TextView mWords; 5 6 public MyLayout (Context context, String title, String words, 7 boolean mVisible) {8 super (context); 9 this. setOrientation (VERTICAL); 10 11 mTitle = new TextView (context); 12 mTitle. setText (title); 13 addView (mTitle, new LinearLayout. layoutParams (14 LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT )) ; 15 16 mWords = new TextView (context); 17 mWords. setText (words); 18 addView (mWords, new LinearLayout. layoutParams (19 LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT); 20 mWords. setVisibility (mVisible? View. VISIBLE: View. GONE); 21} 22 23 public void setTitle (String title) {24 mTitle. setText (title); 25} 26 27 public void setWords (String words) {28 mWords. setText (words); 29} 30 31 public void setWordsVisible (boolean mVisible) {32 mWords. setVisibility (mVisible? View. VISIBLE: View. GONE); 33} 34 35}MyLayout

 

* Inherit from LinearLayout and add two textviews in it.MVisible? View. VISIBLE: View. GONEDetermines whether the content is displayed. Tips: The invisible attribute is not used here.

 

1 class MyBaseAdapter extends BaseAdapter {2 private String [] titles = {"Title 1", "title 2", "Title 3", "Title 4", "Title 5 "}; 3 private String [] words = {"I'm content 1, I'm content 1", "I'm content 2, I'm content 2", "I'm content 3, I am content 3 ", 4" I am content 4, I am content 4 "," I am content 5, I am content 5 "}; 5 private boolean [] flags = {false, false}; 6 private Context mContext; 7 8 public MyBaseAdapter (Context context) {9 mContext = context; 10} 11 12 @ Override13 Public int getCount () {14 // TODO Auto-generated method stub15 return titles. length; 16} 17 18 @ Override19 public Object getItem (int position) {20 // TODO Auto-generated method stub21 return position; 22} 23 24 @ Override25 public long getItemId (int position) {26 // TODO Auto-generated method stub27 return position; 28} 29 30 @ Override31 public View getView (int position, view convertView, ViewGroup pa Rent) {32 // TODO Auto-generated method stub33 MyLayout mLayout; 34 if (convertView = null) {35 mLayout = new MyLayout (mContext, titles [position], words [position], 36 flags [position]); 37} else {38 mLayout = (MyLayout) convertView; 39 mLayout. setTitle (titles [position]); 40 mLayout. setWords (words [position]); 41 mLayout. setWordsVisible (flags [position]); 42} 43 return mLayout; 44} 45 46 public void toggler (I Nt position) {47 flags [position] =! Flags [position]; 48 notifyDataSetChanged (); 49} 50 51 52 53}MyBaseAdapter

 

* Rewrite the getView () method to complete initialization.

 

1 public class BaseAdapterActivity extends ListActivity {2 3 @ Override 4 protected void onCreate (Bundle savedInstanceState) {5 // TODO Auto-generated method stub 6 super. onCreate (savedInstanceState); 7 this. setListAdapter (new MyBaseAdapter (this); 8} 9 10 @ Override11 protected void onListItemClick (ListView l, View v, int position, long id) {12 (MyBaseAdapter) getListAdapter ()). toggler (position); 13} 14 15}MainActivity

 

Related Article

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.