Android ListView Adapter – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

這段時間,由於在項目中經常要用到ListView控件,所以對ListView控件的自定義做瞭一些瞭解 ,其實listView控件的使用關鍵是adapter的使用

 

直接上代碼

 

package com.Adapters;

 

import java.util.ArrayList;

import java.util.HashMap;

 

import com.AppClient.R;

 

 

 

import android.content.Context;

 

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

 

import android.widget.BaseAdapter;

 

import android.widget.ImageView;

import android.widget.TextView;

 

 

public class RecipeAdapter extends BaseAdapter{

 

      

       private LayoutInflater layoutInflater; 

    private Context context; 

   

 

      

       /**

        * 構造函數,配置參數

        * @param data 配置ArrayList<HashMap<String,Object>類型的參數>

        * @param context this*/

       public RecipeAdapter(ArrayList<HashMap<String, Object>> data,

                      Context context) {

              super();

              this.data = data;

              this.layoutInflater = layoutInflater.from(context);

              this.context = context;

       }

      

      

      

 

       private ArrayList<HashMap<String, Object>> data; 

    /**

     * LayoutInflater 類是代碼實現中獲取佈局文件的主要形式

     *LayoutInflater layoutInflater = LayoutInflater.from(context);

     *View convertView = layoutInflater.inflate();

     *LayoutInflater的使用,在實際開發種LayoutInflater這個類還是非常有用的,它的作用類似於findViewById(),

                  不同點是LayoutInflater是用來找layout下xml佈局文件,並且實例化!

                而findViewById()是找具體xml下的具體widget控件(如:Button,TextView等)。

     */ 

 

   

   

   

       @Override

       public int getCount() {

              // TODO Auto-generated method stub

              return data.size();

             

       }

 

       @Override

       public Object getItem(int position) {

              // TODO Auto-generated method stub

              return data.get(position);

       }

 

       @Override

       public long getItemId(int position) {

              // TODO Auto-generated method stub

              return position;

       }

   

      

        /**

     * android繪制每一列的時候,都會調用這個方法

     */

       @Override

       public View getView(int position, View convertView, ViewGroup parent) {

              // TODO Auto-generated method stub

              Controlors controlor=null;

              if(convertView==null)

              {

                     controlor=new Controlors();

                     //獲取佈局組件

                     convertView=layoutInflater.inflate(R.layout.recipe_list, null);

                     controlor.imageView=(ImageView)convertView.findViewById(R.id.recipe_image);

                     controlor.textView=(TextView)convertView.findViewById(R.id.recipe_name);

                     controlor.writer=(TextView)convertView.findViewById(R.id.writer);

                     controlor.mainResource=(TextView)convertView.findViewById(R.id.main_resource);

                     controlor.littleResourc=(TextView)convertView.findViewById(R.id.little_resource);

                     //使用tag來存儲數據

                     convertView.setTag(controlor);

              }

              else

              {

                     controlor=(Controlors)convertView.getTag();

              }

              //綁定數據以及事件觸發

              controlor.imageView.setBackgroundResource((Integer)data.get(position).get("recipe_image"));

              controlor.textView.setText((String)data.get(position).get("recipe_name"));

           controlor.writer.setText((String)data.get(position).get("writer"));

           controlor.mainResource.setText((String)data.get(position).get("main_resource"));

           controlor.littleResourc.setText((String)data.get(position).get("little_resource"));

              return convertView;

       }

      

      

 

}

 

You May Also Like