Newer
Older
dxcgt / app / src / main / java / com / smartdot / cgt / view / BaseModelAdapter.java
wangxitong on 6 Apr 2021 3 KB first commit
/**
 * 自定义ListView适配器
 */
package com.smartdot.cgt.view;

import java.util.List;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartdot.cgt.model.BaseModel;
import com.smartdot.cgt.util.ApplicationMain;
import com.smartdot.cgt.util.StringUtils;
import com.smartdot.cgt.R;

/**
 * @author Administrator
 * 
 */
public class BaseModelAdapter<T extends BaseModel> extends BaseAdapter {

    protected Context context;

    private List < T > data;

    private int resource;

    private String[] from;

    private int[] to;

    /**
	 * 
	 */
    public BaseModelAdapter(Context context, List < T > data, int resource, String[] from, int[] to) {
        this.context = context;
        this.data = data;
        this.resource = resource;
        this.from = from;
        this.to = to;
    }

    public void addData(List < T > listData) {
        if (data != null) {
            data.addAll(listData);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.Adapter#getCount()
     */
    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.Adapter#getItem(int)
     */
    @Override
    public Object getItem(int arg0) {
        T item = null;
        if (data != null && data.size() > 0) {
            item = data.get(arg0);
        }
        return item;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.Adapter#getItemId(int)
     */
    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.Adapter#getView(int, android.view.View,
     * android.view.ViewGroup)
     */
    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        View view = arg1;
        if (view == null) {
            view = ApplicationMain.getInstance().inflateView(this.resource);
        }
        T item = data.get(arg0);
        Class < ? extends BaseModel > clazz = item.getClass();
        int fieldLength = from.length;
        for (int i = 0; i < fieldLength; i++) {
            Object value = item.get(from[i], clazz);
            View fieldView = view.findViewById(to[i]);
            if (fieldView instanceof TextView) {
                TextView fieldText = (TextView) fieldView;
                fieldText.setText(null == value ? "" : value.toString());
            } else if (fieldView instanceof ImageView) {
                if (null != value && !StringUtils.isNullOrEmpty(value.toString())) {
                    ImageView fieldImage = (ImageView) fieldView;
                   if("部件".equals(value))
                   {
                	   fieldImage.setImageResource(R.drawable.icon_task1);	
                	   //fieldImage.setImageURI(Uri.parse(value.toString()));	
                   }else
                   {
                	   fieldImage.setImageResource(R.drawable.icon_task);	
                	  // fieldImage.setImageURI(Uri.parse(value.toString()));   
                   }
                    
                }
            }
        }
        return view;
    }
}