Customizing the ListView Adapter

A custom MyAdapter

public  class MyAdapter the extends BaseAdapter { 

    // Adapter acts corresponds to the line from a data layout mapping. This requires the data to be displayed incoming List 
    List <the Map <String, Object >> List;
     // reflector 
    LayoutInflater inflater so that; 


    // initialize reflector 
    public MyAdapter (the Context context) {
         the this .inflater = LayoutInflater.from (context) ; 
    } 

    // this method initializes List 
    public  void setList (List <the Map <String, Object >> List) {
         the this .list = List; 
    } 

    // if return to the default of 0, no matter what you pass List, not all display
     //Back list.size (); it is just the number of display list corresponding to the length. 
    @Override
     public  int getCount () {
         return list.size (); 
    } 

    // because List is used, this is how to set how the data obtained from List List of index 
    @Override
     public Object the getItem ( int position) {
         return list.get (position); 
    } 

    // current id is how much of this one? List id is an index for each position 
    @Override
     public  Long getItemId ( int position) {
         return position; 
    } 

    // page rendering rendering line by line from time to time, from the start position 0. If he returns here to empty, nothing is displayed on the page. 
    @Override
    public View the getView ( int position, View convertView, the ViewGroup parent) {
         // reflector role here, the reflection of a text file as an xml View object.
        // This is the method of two parameters, there are other types of process parameters
         // first parameter id is passed in line layout corresponding
         // The second parameter to be passed in the parent container. Actual use, may not pass normal operation can 
        View View = Inflater.inflate (R.layout.item, null ); 

        // the findViewById the method previously used directly, not added in front of the object name. In fact, the default is to this.findViewById
         // current, view the layout of this trip has become through reflection. May be obtained which lower the control by 
        the ImageView logo = view.findViewById (R.id.logo); 
        the TextView title = view.findViewById (R.id.title); 
        the TextView Version = view.findViewById(R.id.version);
        TextView size = view.findViewById(R.id.size);

        //针对当前position将list的数据传给view
        Map map = list.get(position);
        logo.setImageResource((Integer) map.get("logo"));
        title.setText((String) map.get("title"));
        version.setText((String) map.get("version"));
        size.setText((String) map.get("size"));

        return view;
    }
}

Second, the use of the MainActivity

        MyAdapter adapter = new MyAdapter(this);
        adapter.setList(list);
        lv.setAdapter(adapter);

 

Guess you like

Origin www.cnblogs.com/xxie12/p/11484406.html