ListView for android (list)

Source: Internet
Author: User

There are several ways to display the list effect on the android interface:

Method 1: Inherit from ListActivity. The key code for using Listadapter is as follows:

private ListAdapter mListAdapter;private List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);listItems=getListItems();mListAdapter = new ListAdapter(this,listItems);setListAdapter(mListAdapter);}private List<Map<String, Object>> getListItems() {         Map<String, Object> map = new HashMap<String, Object>();         map.put("mytitle","Oracle" );         map.put("myimage", R.drawable.a);         map.put("myimages", R.drawable.rating_5);         listItems.add(map);          map = new HashMap<String, Object>();         map.put("mytitle","Sqlite" );         map.put("myimage", R.drawable.b);         map.put("myimages", R.drawable.rating_5);         listItems.add(map);          map = new HashMap<String, Object>();         map.put("mytitle","MySql" );         map.put("myimage", R.drawable.c);         map.put("myimages", R.drawable.rating_5);         listItems.add(map);            return listItems;}

ListAdapter code:

Package org. anjoy. act; import java. util. list; import java. util. map; import android. app. alertDialog; import android. content. context; import android. content. dialogInterface; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. textView; public Class ListAdapter extends BaseAdapter {private Context context; // Context path private LayoutInflater inflater; // view container private List <Map <String, Object> listitems; // item information set public class holderView // custom control set {private TextView mytitle; private ImageView myimage; private ImageView myimages;} // construct the DeviceAdapter method public ListAdapter (Context context, list <Map <String, Object> listitems) {this. context = context; this. inflater = L AyoutInflater. from (context); // create a view container this. listitems = listitems;} public int getCount () {// TODO Auto-generated method stubreturn listitems. size ();} public Object getItem (int arg0) {// TODO Auto-generated method stubreturn listitems. get (arg0);} public long getItemId (int arg0) {// TODO Auto-generated method stubreturn arg0;} public View getView (int position, View convertView, ViewGroup parent) {// custom view ho LderView mView = null; if (convertView = null) {mView = new holderView (); // obtain the listitem layout file convertView = inflater. inflate (R. layout. main, null); // gets the control object mView. mytitle = (TextView) convertView. findViewById (R. id. mytitle); mView. myimage = (ImageView) convertView. findViewById (R. id. myimage); mView. myimages = (ImageView) convertView. findViewById (R. id. myimageview); // set the control object to convertview in the convertView Gallery. setTag (mView);} else {mView = (HolderView) convertView. getTag ();} // set the value of mView on the item control. mytitle. setText (String) listitems. get (position ). get ("mytitle"); mView. myimage. setBackgroundResource (Integer) listitems. get (position ). get ("myimage"); mView. myimages. setBackgroundResource (Integer) listitems. get (position ). get ("myimages"); mView. myimage. setOnClickListener (new OnClickListener () {public void onClick (View v) {// TODO Auto-generat Ed method stubnew AlertDialog. Builder (context). setIcon (R. drawable. alert). setTitle ("tip"). setMessage ("Please take a look! "). SetPositiveButton (" OK ", null). show () ;}}); return convertView ;}}

:

3. Method 2: The activity inherits the Listview and uses SimpleAdapter and the layout file.

Key code:

@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); SimpleAdapter adapter = new SimpleAdapter (this, getData (), R. layout. main, new String [] {"mytitle", "myimageview", "myimage"}, new int [] {R. id. mytitle, R. id. myimageview, R. id. myimage}); setListAdapter (adapter);} private List <Map <String, Object> getData () {List <Map <String, object> list = new ArrayList <Map <String, Object> (); Map <String, Object> map = new HashMap <String, Object> (); map. put ("mytitle", "IOS textbook"); map. put ("myimageview", R. drawable. rating_5); map. put ("myimage", R. drawable. a); list. add (map); map = new HashMap <String, Object> (); map. put ("mytitle", "Android textbook"); map. put ("myimageview", R. drawable. rating_5); map. put ("myimage", R. drawable. b); list. add (map); map = new HashMap <String, Object> (); map. put ("mytitle", "java textbook"); map. put ("myimageview", R. drawable. rating_5); map. put ("myimage", R. drawable. c); list. add (map); return list ;}

Above

2. Method 3: Create a ListView object in activity and use ListAdapter

@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); SimpleAdapter adapter = new SimpleAdapter (this, getData (), R. layout. main, new String [] {"mytitle", "myimageview", "myimage"}, new int [] {R. id. mytitle, R. id. myimageview, R. id. myimage}); setListAdapter (adapter);} private List <Map <String, Object> getData () {List <Map <String, object> list = new ArrayList <Map <String, Object> (); Map <String, Object> map = new HashMap <String, Object> (); map. put ("mytitle", "IOS textbook"); map. put ("myimageview", R. drawable. rating_5); map. put ("myimage", R. drawable. a); list. add (map); map = new HashMap <String, Object> (); map. put ("mytitle", "Android textbook"); map. put ("myimageview", R. drawable. rating_5); map. put ("myimage", R. drawable. b); list. add (map); map = new HashMap <String, Object> (); map. put ("mytitle", "java textbook"); map. put ("myimageview", R. drawable. rating_5); map. put ("myimage", R. drawable. c); list. add (map); return list ;}

As follows:

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.