com.app.jdy.adapter.DetailAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.app.jdy.adapter.DetailAdapter.java

Source

/**
 * Copyright (c) 2015
 *
 * Licensed under the UCG License, Version 1.0 (the "License");
 */
package com.app.jdy.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONException;
import org.json.JSONObject;

import com.app.jdy.R;
import com.app.jdy.ui.ImagePagerActivity;
import com.app.jdy.utils.BitmapUtils;
import com.app.jdy.utils.URLs;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
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.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * description :
 * 
 * @version 1.0
 * @author zhoufeng
 * @createtime : 2015-1-10 ?9:25:04
 * 
 *             ?:    --------------- -------------------
 *             ----------------------------------- zhoufeng 2015-1-10 ?9:25:04
 * 
 */
public class DetailAdapter extends BaseAdapter {
    /**
     * ?
     */
    private Context context;
    /**
     * 
     */
    private LayoutInflater listContainer;
    /**
     * ?
     */
    private int itemViewResource;
    /**
     * json?
     */
    private String dataJson;
    /**
     * linearyout
     */
    private LinearLayout detail_linearyout;
    /**
     * ?title textview
     */
    private TextView detail_item_title;
    /**
     * ?text textview
     */
    private TextView detail_item_text;

    JSONObject jsonObject;

    JSONObject product;

    JSONObject productChild;
    /**
     * ?
     */
    private Boolean isSetColor = true;
    /**
     * 
     */
    private Boolean isFlag = false;
    /**
     * 
     */
    private ImageView detail_image_item_text;

    private ArrayList bankList;

    private ArrayList insuranceList;

    private ArrayList pubFundsList;

    private ArrayList priFundsList;

    private ArrayList trustList;

    private ArrayList captManageList;

    private ArrayList creditList;

    private ArrayList equityList;

    private HashMap<String, String> bankMap;

    private HashMap<String, String> insuranceMap;

    private HashMap<String, String> pubFundsMap;

    private HashMap<String, String> priFundsMap;

    private HashMap<String, String> trustMap;

    private HashMap<String, String> captManageMap;

    private HashMap<String, String> creditMap;

    private HashMap<String, String> equityMap;

    private Bitmap logo;

    private Bitmap prodImage;

    public DetailAdapter(Context context, int resource, String dataJson, Bitmap logo, Bitmap prodImage) {
        this.context = context;
        this.listContainer = LayoutInflater.from(context); // 
        this.itemViewResource = resource;
        this.dataJson = dataJson;
        this.logo = logo;
        this.prodImage = prodImage;
        initHashMap();

    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

    @Override
    public int getCount() {
        return 35;
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null || convertView.getTag() == null) {
            // ?list_item
            convertView = listContainer.inflate(this.itemViewResource, null);
            detail_item_title = (TextView) convertView.findViewById(R.id.detail_item_title);
            detail_item_text = (TextView) convertView.findViewById(R.id.detail_item_text);
            detail_linearyout = (LinearLayout) convertView.findViewById(R.id.detail_linearyout);
            try {
                jsonObject = new JSONObject(dataJson);
                product = new JSONObject(jsonObject.getString("product1"));
                productChild = new JSONObject(jsonObject.getString("product2"));
                final String[] urls = { URLs.HTTP + URLs.HOST + product.getString("issuerLogo"),
                        URLs.HTTP + URLs.HOST + product.getString("prodImage") };

                switch (position) {
                case 0:
                    showDetail("???", product.getString("name"));
                    break;
                case 1:
                    showDetail("?", product.getString("issuer"));
                    break;
                case 2:
                    showDetail("?", product.getString("issuerTypeName"));
                    break;
                case 3:
                    convertView = listContainer.inflate(R.layout.detail_image_item, null);
                    detail_item_title = (TextView) convertView.findViewById(R.id.detail_image_item_title);
                    detail_image_item_text = (ImageView) convertView.findViewById(R.id.detail_image_item_text);
                    detail_linearyout = (LinearLayout) convertView.findViewById(R.id.detail_linearyout);
                    ImageLoader.getInstance().displayImage(urls[0], detail_image_item_text);
                    detail_image_item_text.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            imageBrower(0, urls);
                        }
                    });
                    showDetail("?logo", product.getString("issuerLogo"));
                    break;
                case 4:
                    convertView = listContainer.inflate(R.layout.detail_image_item, null);
                    detail_item_title = (TextView) convertView.findViewById(R.id.detail_image_item_title);
                    detail_image_item_text = (ImageView) convertView.findViewById(R.id.detail_image_item_text);
                    detail_linearyout = (LinearLayout) convertView.findViewById(R.id.detail_linearyout);
                    ImageLoader.getInstance().displayImage(urls[1], detail_image_item_text);
                    detail_image_item_text.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            imageBrower(1, urls);
                        }
                    });
                    showDetail("?", product.getString("prodImage"));
                    break;
                case 5:
                    showDetail("?()", product.getString("period"));
                    break;
                case 6:
                    showDetail("", product.getString("profitTypeName"));
                    break;
                case 7:
                    showDetail("??", product.getString("currencyTypeName"));
                    break;
                case 8:
                    showDetail("??", product.getString("statusName"));
                    break;
                case 9:
                    showDetail("?(%)", product.getString("topGain"));
                    break;
                case 10:
                    showDetail("", product.getString("saleStart"));
                    break;
                case 11:
                    showDetail("?", product.getString("saleEnd"));
                    break;
                case 12:
                    showDetail("", product.getString("profitStart"));
                    break;
                case 13:
                    showDetail("?", product.getString("profitEnd"));
                    break;
                case 14:
                    showDetail("?", product.getString("investDirection"));
                    break;
                case 15:
                    showDetail("", product.getString("fundTypeName"));
                    break;
                case 16:
                    showDetail("?", product.getString("fundDirector"));
                    break;
                case 17:
                    showDetail("?", product.getString("insuranceTypeName"));
                    break;
                case 18:
                    choiceType(product.getString("prodTypeCode"), "0", 0);
                    break;
                case 19:
                    choiceType(product.getString("prodTypeCode"), "1", 1);
                    break;
                case 20:
                    choiceType(product.getString("prodTypeCode"), "2", 2);
                    break;
                case 21:
                    choiceType(product.getString("prodTypeCode"), "3", 3);
                    break;
                case 22:
                    choiceType(product.getString("prodTypeCode"), "4", 4);
                    break;
                case 23:
                    choiceType(product.getString("prodTypeCode"), "5", 5);
                    break;
                case 24:
                    choiceType(product.getString("prodTypeCode"), "6", 6);
                    break;
                case 25:
                    choiceType(product.getString("prodTypeCode"), "7", 7);
                    break;
                case 26:
                    choiceType(product.getString("prodTypeCode"), "8", 8);
                    break;
                case 27:
                    choiceType(product.getString("prodTypeCode"), "9", 9);
                    break;
                case 28:
                    choiceType(product.getString("prodTypeCode"), "10", 10);
                    break;
                case 29:
                    choiceType(product.getString("prodTypeCode"), "11", 11);
                    break;
                case 30:
                    choiceType(product.getString("prodTypeCode"), "12", 12);
                    break;
                case 31:
                    choiceType(product.getString("prodTypeCode"), "13", 13);
                    break;
                case 32:
                    choiceType(product.getString("prodTypeCode"), "14", 14);
                    break;
                case 33:
                    choiceType(product.getString("prodTypeCode"), "15", 15);
                    break;
                case 34:
                    choiceType(product.getString("prodTypeCode"), "16", 16);
                    break;
                case 35:
                    choiceType(product.getString("prodTypeCode"), "17", 17);
                    break;

                default:
                    break;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return convertView;
    }

    public void choiceType(String type, String code, int key) throws JSONException {
        if (type.equals("bank")) {
            selectDetail(bankMap, bankList, code, key, 4);
        } else if (type.equals("insurance")) {
            selectDetail(insuranceMap, insuranceList, code, key, 3);
        } else if (type.equals("pubFunds")) {
            selectDetail(pubFundsMap, pubFundsList, code, key, 17);
        } else if (type.equals("priFunds")) {
            selectDetail(priFundsMap, priFundsList, code, key, 18);
        } else if (type.equals("trust")) {
            selectDetail(trustMap, trustList, code, key, 4);
        } else if (type.equals("captManage")) {
            selectDetail(captManageMap, captManageList, code, key, 4);
        } else if (type.equals("credit")) {
            selectDetail(creditMap, creditList, code, key, 7);
        } else if (type.equals("equity")) {
            selectDetail(equityMap, equityList, code, key, 7);
        }
    }

    public void initHashMap() {
        bankList = new ArrayList();
        bankList.add("subsFee");
        bankList.add("prodType");
        bankList.add("entrustLimit");
        bankList.add("entrustIncrease");
        insuranceList = new ArrayList();
        insuranceList.add("breakEven");
        insuranceList.add("backDate");
        insuranceList.add("suitablePeople");
        pubFundsList = new ArrayList();
        pubFundsList.add("shortName");
        pubFundsList.add("newestWorth");
        pubFundsList.add("totalWorth");
        pubFundsList.add("fundCode");
        pubFundsList.add("expectProfit");
        pubFundsList.add("fundManager");
        pubFundsList.add("createDate");
        pubFundsList.add("fiirstRaise");
        pubFundsList.add("newestRaise");
        pubFundsList.add("applyAtoneStatus");
        pubFundsList.add("applyAtoneTypeName");
        pubFundsList.add("applyRate");
        pubFundsList.add("atoneRate");
        pubFundsList.add("manageRate");
        pubFundsList.add("investTarget");
        pubFundsList.add("captialRate");
        pubFundsList.add("profitPrinciple");
        priFundsList = new ArrayList();
        priFundsList.add("shortName");
        priFundsList.add("unitWorth");
        priFundsList.add("newestWorth");
        priFundsList.add("totalWorth");
        priFundsList.add("createDate");
        priFundsList.add("openDate");
        priFundsList.add("endDate");
        priFundsList.add("closedPeriod");
        priFundsList.add("standClosedPeriod");
        priFundsList.add("subsRate");
        priFundsList.add("manageRate");
        priFundsList.add("redemptionRate");
        priFundsList.add("depositBank");
        priFundsList.add("unstableRate");
        priFundsList.add("limitBuy");
        priFundsList.add("pubArea");
        priFundsList.add("investTactics");
        priFundsList.add("fundScope");
        trustList = new ArrayList();
        trustList.add("trustType");
        trustList.add("saleDate");
        trustList.add("pubArea");
        trustList.add("projectArea");
        captManageList = new ArrayList();
        captManageList.add("capType");
        captManageList.add("saleDate");
        captManageList.add("pubArea");
        captManageList.add("projectArea");
        creditList = new ArrayList();
        creditList.add("borrowPurpose");
        creditList.add("targetTotal");
        creditList.add("repayPeriod");
        creditList.add("ensureType");
        creditList.add("repaymentType");
        creditList.add("borrowProtocol");
        creditList.add("projectArea");
        equityList = new ArrayList();
        equityList.add("borrowPurpose");
        equityList.add("targetTotal");
        equityList.add("repayPeriod");
        equityList.add("ensureType");
        equityList.add("repaymentType");
        equityList.add("borrowProtocol");
        equityList.add("projectArea");

        bankMap = new HashMap<String, String>();
        bankMap.put("0", "()");
        bankMap.put("1", "?");
        bankMap.put("2", "?()");
        bankMap.put("3", "???()");
        insuranceMap = new HashMap<String, String>();
        insuranceMap.put("0", "?");
        insuranceMap.put("1", "");
        insuranceMap.put("2", "");
        pubFundsMap = new HashMap<String, String>();
        pubFundsMap.put("0", "");
        pubFundsMap.put("1", "()");
        pubFundsMap.put("2", "()");
        pubFundsMap.put("3", "?");
        pubFundsMap.put("4", "");
        pubFundsMap.put("5", "??");
        pubFundsMap.put("6", "?");
        pubFundsMap.put("7", "()");
        pubFundsMap.put("8", "()");
        pubFundsMap.put("9", "?");
        pubFundsMap.put("10", "???");
        pubFundsMap.put("11", "(%)");
        pubFundsMap.put("12", "(%)");
        pubFundsMap.put("13", "?(%)");
        pubFundsMap.put("14", "");
        pubFundsMap.put("15", "?");
        pubFundsMap.put("16", "?");
        priFundsMap = new HashMap<String, String>();
        priFundsMap.put("0", "");
        priFundsMap.put("1", "??()");
        priFundsMap.put("2", "()");
        priFundsMap.put("3", "()");
        priFundsMap.put("4", "?");
        priFundsMap.put("5", "");
        priFundsMap.put("6", "");
        priFundsMap.put("7", "?");
        priFundsMap.put("8", "??()");
        priFundsMap.put("9", "");
        priFundsMap.put("10", "?(%)");
        priFundsMap.put("11", "(%)");
        priFundsMap.put("12", "");
        priFundsMap.put("13", "?(%)");
        priFundsMap.put("14", "()");
        priFundsMap.put("15", "?");
        priFundsMap.put("16", "");
        priFundsMap.put("17", "");
        trustMap = new HashMap<String, String>();
        trustMap.put("0", "");
        trustMap.put("1", "?");
        trustMap.put("2", "?");
        trustMap.put("3", "");
        captManageMap = new HashMap<String, String>();
        captManageMap.put("0", "");
        captManageMap.put("1", "?");
        captManageMap.put("2", "?");
        captManageMap.put("3", "");
        creditMap = new HashMap<String, String>();
        creditMap.put("0", "");
        creditMap.put("1", "?()");
        creditMap.put("2", "?()");
        creditMap.put("3", "?");
        creditMap.put("4", "??");
        creditMap.put("5", "??");
        creditMap.put("6", "");
        equityMap = new HashMap<String, String>();
        equityMap.put("0", "");
        equityMap.put("1", "?()");
        equityMap.put("2", "?()");
        equityMap.put("3", "??");
        equityMap.put("4", "?");
        equityMap.put("5", "??");
        equityMap.put("6", "");
    }

    public void selectDetail(HashMap<String, String> map, ArrayList list, String code, int key, int num)
            throws JSONException {
        if (key < num) {
            showDetail(map.get(code), productChild.getString(list.get(key).toString()));
        } else {
            showDetail(map.get(code), "null");
        }
    }

    public void showDetail(String titleName, String textName) {
        if (!textName.equals("null") && !textName.equals("")) {
            detail_item_title.setText(titleName);

            if (titleName.equals("?logo")) {
                //            detail_image_item_text.setImageBitmap(logo);
                detail_linearyout.setVisibility(View.GONE);
            }
            if (titleName.equals("?")) {
                detail_linearyout.setVisibility(View.GONE);
                //            detail_image_item_text.setImageBitmap(prodImage);
            } else {
                detail_item_text.setText(textName);
            }

            if (isFlag) {
                isSetColor = false;
            } else {
                isSetColor = true;
            }
            isFlag = false;
            if (isSetColor) {
                detail_item_title.setBackgroundResource(R.drawable.item_color);
                if (titleName.equals("?logo") || titleName.equals("?")) {
                    detail_image_item_text.setBackgroundResource(R.drawable.item_color);
                } else {
                    detail_item_text.setBackgroundResource(R.drawable.item_color);
                }

                isSetColor = false;
                isFlag = true;
            }
        } else {
            detail_linearyout.setVisibility(View.GONE);
            if (isSetColor) {
                isSetColor = false;
            } else {
                isSetColor = true;
            }
        }

    }

    private void imageBrower(int position, String[] urls) {
        Intent intent = new Intent(context, ImagePagerActivity.class);
        // url,???
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
        context.startActivity(intent);
    }
}