com.df.app.carsChecked.BasicInfoLayout.java Source code

Java tutorial

Introduction

Here is the source code for com.df.app.carsChecked.BasicInfoLayout.java

Source

package com.df.app.carsChecked;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.df.app.MainActivity;
import com.df.app.R;
import com.df.library.entries.Brand;
import com.df.library.entries.Country;
import com.df.library.entries.Manufacturer;
import com.df.library.entries.Model;
import com.df.library.entries.Series;
import com.df.library.util.MyScrollView;

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

import static com.df.library.util.Helper.setTextView;

/**
 * Created by  on 14-3-14.
 */
public class BasicInfoLayout extends LinearLayout {
    private ViewGroup rootView;

    public BasicInfoLayout(Context context, JSONObject procedures, String seriesId, String modelId) {
        super(context);
        init(context, procedures, seriesId, modelId);
    }

    public BasicInfoLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BasicInfoLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private void init(Context context, JSONObject procedures, String seriesId, String modelId) {
        rootView = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.car_report_basic_layout, this);

        MyScrollView scrollView = (MyScrollView) findViewById(R.id.root);
        scrollView.setListener(new MyScrollView.ScrollViewListener() {
            @Override
            public void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy) {
                if (scrollView.getScrollY() > 5) {
                    showShadow(true);
                } else {
                    showShadow(false);
                }
            }
        });

        // ??????
        scrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
        scrollView.setFocusable(true);
        scrollView.setFocusableInTouchMode(true);
        scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.requestFocusFromTouch();
                return false;
            }
        });

        try {
            fillInData(procedures, seriesId, modelId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void showShadow(boolean show) {
        findViewById(R.id.shadow).setVisibility(show ? VISIBLE : INVISIBLE);
    }

    private View findView(ViewGroup viewGroup, String tag) {
        View view = null;

        for (int i = 0; i < viewGroup.getChildCount(); i++) {

            view = viewGroup.getChildAt(i);

            if (view instanceof ViewGroup) {
                view = findView((ViewGroup) view, tag);

                if (tag.equals(view.getTag())) {
                    return view;
                }
            } else {
                if (tag.equals(view.getTag())) {
                    return view;
                }
            }
        }

        return view;
    }

    private void fillInData(JSONObject procedures, String seriesId, String modelId) throws JSONException {

        //        Iterator iterator = procedures.keys();
        //        try {
        //            while (iterator.hasNext()) {
        //                String temp = (String)iterator.next();
        //
        //                TextView textView = (TextView)findView(rootView, temp);
        //                if(textView != null) {
        //                    textView.setText(procedures.get(temp) == JSONObject.NULL ? "" : procedures.getString(temp));
        //                }
        //            }
        //        } catch (Exception e) {
        //            e.printStackTrace();
        //        }

        setTextView(rootView, R.id.vehicleType_text,
                procedures.get("licensetype") == JSONObject.NULL ? "" : procedures.getString("licensetype"));
        setTextView(rootView, R.id.regDate_text,
                procedures.get("regdate") == JSONObject.NULL ? "" : procedures.getString("regdate"));
        setTextView(rootView, R.id.builtDate_text, procedures.get("leavefactorydate") == JSONObject.NULL ? ""
                : procedures.getString("leavefactorydate"));
        setTextView(rootView, R.id.plateNumber_text,
                procedures.get("license") == JSONObject.NULL ? "" : procedures.getString("license"));
        setTextView(rootView, R.id.exteriorColor_text,
                procedures.get("color") == JSONObject.NULL ? "" : procedures.getString("color"));
        setTextView(rootView, R.id.mileage_text,
                procedures.get("mileage") == JSONObject.NULL ? "" : procedures.getString("mileage"));
        setTextView(rootView, R.id.vin_text,
                procedures.get("vin") == JSONObject.NULL ? "" : procedures.getString("vin"));
        setTextView(rootView, R.id.engineSerial_text,
                procedures.get("engineno") == JSONObject.NULL ? "" : procedures.getString("engineno"));
        setTextView(rootView, R.id.licenseModel_text,
                procedures.get("model2") == JSONObject.NULL ? "" : procedures.getString("model2"));
        setTextView(rootView, R.id.ownerName_text,
                procedures.get("owner") == JSONObject.NULL ? "" : procedures.getString("owner"));
        setTextView(rootView, R.id.ownerIdNumber_text,
                procedures.get("ownerid") == JSONObject.NULL ? "" : procedures.getString("ownerid"));
        setTextView(rootView, R.id.ownerPhone_text,
                procedures.get("ownermobile") == JSONObject.NULL ? "" : procedures.getString("ownermobile"));
        setTextView(rootView, R.id.regArea_text,
                procedures.get("regarea") == JSONObject.NULL ? "" : procedures.getString("regarea"));
        setTextView(rootView, R.id.transferCount_text,
                procedures.get("tradetimes") == JSONObject.NULL ? "" : procedures.getString("tradetimes"));
        setTextView(rootView, R.id.violationRecords_text,
                procedures.get("wzjl") == JSONObject.NULL ? "" : procedures.getString("wzjl"));
        setTextView(rootView, R.id.useCharacter_text,
                procedures.get("kind") == JSONObject.NULL ? "" : procedures.getString("kind"));
        setTextView(rootView, R.id.originUser_text,
                procedures.get("kind2") == JSONObject.NULL ? "" : procedures.getString("kind2"));
        setTextView(rootView, R.id.namePlate_text,
                procedures.get("csmp") == JSONObject.NULL ? "" : procedures.getString("csmp"));
        setTextView(rootView, R.id.import_text,
                procedures.get("sfjk") == JSONObject.NULL ? "" : procedures.getString("sfjk"));

        setTextView(rootView, R.id.importWay_text,
                procedures.get("gmfs") == JSONObject.NULL ? "" : procedures.getString("gmfs"));
        setTextView(rootView, R.id.importEntryBill_text,
                procedures.get("jkgd") == JSONObject.NULL ? "" : procedures.getString("jkgd"));
        setTextView(rootView, R.id.importBusinessCheck_text,
                procedures.get("sjd") == JSONObject.NULL ? "" : procedures.getString("sjd"));
        setTextView(rootView, R.id.licencePhotoMatch_text,
                procedures.get("sxdd") == JSONObject.NULL ? "" : procedures.getString("sxdd"));
        setTextView(rootView, R.id.driveLicence_text, procedures.get("drivinglicense") == JSONObject.NULL ? ""
                : procedures.getString("drivinglicense"));
        setTextView(rootView, R.id.regLicence_text,
                procedures.get("regcert") == JSONObject.NULL ? "" : procedures.getString("regcert"));
        setTextView(rootView, R.id.invoice_text,
                procedures.get("invoice") == JSONObject.NULL ? "" : procedures.getString("invoice"));
        setTextView(rootView, R.id.surtax_text,
                procedures.get("surtax") == JSONObject.NULL ? "" : procedures.getString("surtax"));
        setTextView(rootView, R.id.maintenanceManual_text,
                procedures.get("manual") == JSONObject.NULL ? "" : procedures.getString("manual"));
        setTextView(rootView, R.id.vehicleTax_text,
                procedures.get("cctax") == JSONObject.NULL ? "" : procedures.getString("cctax"));
        setTextView(rootView, R.id.annualInspectionDate_text,
                procedures.get("njdate") == JSONObject.NULL ? "" : procedures.getString("njdate"));
        setTextView(rootView, R.id.spareKey_text,
                procedures.get("key") == JSONObject.NULL ? "" : procedures.getString("key"));
        setTextView(rootView, R.id.compulsoryInsurance_text,
                procedures.get("jqx") == JSONObject.NULL ? "" : procedures.getString("jqx"));
        setTextView(rootView, R.id.compulsoryInsuranceDate_text,
                procedures.get("jqdate") == JSONObject.NULL ? "" : procedures.getString("jqdate"));

        JSONObject sx = procedures.getJSONObject("sx");
        setTextView(rootView, R.id.insurance_text,
                sx.getString("Value") == JSONObject.NULL ? "" : sx.getString("Value"));

        setTextView(rootView, R.id.insuranceRegion_text,
                procedures.get("sxdy") == JSONObject.NULL ? "" : procedures.getString("sxdy"));
        setTextView(rootView, R.id.insuranceAmount_text,
                procedures.get("sxmoney") == JSONObject.NULL ? "" : procedures.getString("sxmoney"));
        setTextView(rootView, R.id.insuranceExpiryDate_text,
                procedures.get("sxdate") == JSONObject.NULL ? "" : procedures.getString("sxdate"));

        setTextView(rootView, R.id.insuranceCompany_text,
                procedures.get("insurer") == JSONObject.NULL ? "" : procedures.getString("insurer"));

        if (procedures.get("source") == JSONObject.NULL) {
            setTextView(rootView, R.id.source_text, "");
        } else {
            JSONObject source = procedures.getJSONObject("source");
            String sourceString = "";
            if (source.has("f1") && !source.isNull("f1")) {
                sourceString += source.getJSONObject("f1").getString("Value");
            } else {
                sourceString = "";
            }

            // f1f2
            if (source.has("f2") && !source.isNull("f2")) {
                sourceString += " - ";
                sourceString += source.getJSONObject("f2").getString("Value");
            }

            // f2f3
            if (source.has("f3") && !source.isNull("f3")) {
                sourceString += " - ";
                sourceString += source.getJSONObject("f3").getString("Value");
            }

            setTextView(rootView, R.id.source_text, sourceString);
        }

        setTextView(rootView, R.id.seller_text,
                procedures.get("xsry") == JSONObject.NULL ? "" : procedures.getString("xsry"));
        setTextView(rootView, R.id.carProperty_text, procedures.get("CarAttributeName") == JSONObject.NULL ? ""
                : procedures.getString("CarAttributeName"));
        setTextView(rootView, R.id.exchangeRequirement_text,
                procedures.get("zhxq") == JSONObject.NULL ? "" : procedures.getString("zhxq"));
        setTextView(rootView, R.id.exchangeCarModel_text,
                procedures.get("zhxh") == JSONObject.NULL ? "" : procedures.getString("zhxh"));

        // ??
        Country country = null;
        Brand brand = null;
        Manufacturer manufacturer = null;
        Series series = null;
        Model model = null;

        boolean found = false;

        for (Country country1 : MainActivity.vehicleModel.getCountries()) {
            for (Brand brand1 : country1.brands) {
                for (Manufacturer manufacturer1 : brand1.manufacturers) {
                    for (Series series1 : manufacturer1.serieses) {
                        if (series1.id.equals(seriesId)) {
                            manufacturer = manufacturer1;
                            brand = brand1;
                            country = country1;
                            series = series1;
                            model = series.getModelById(modelId);

                            found = true;
                            break;
                        }
                    }
                    if (found)
                        break;
                }
                if (found)
                    break;
            }
            if (found)
                break;
        }

        // 
        String brandString = manufacturer.name + " " + series.name + " " + model.name;

        setTextView(rootView, R.id.brandText, brandString);
    }
}