com.huishen.edrive.center.CoachMealListFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.huishen.edrive.center.CoachMealListFragment.java

Source

package com.huishen.edrive.center;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshExpandableListView;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huishen.edrive.R;
import com.huishen.edrive.net.DefaultErrorListener;
import com.huishen.edrive.net.NetUtil;
import com.huishen.edrive.net.SRL;
import com.huishen.edrive.util.AppUtil;
import com.huishen.edrive.widget.BaseFragment;
import com.huishen.edrive.widget.LoadingView;
import com.huishen.edrive.widget.TitleListFragment;

public class CoachMealListFragment extends BaseFragment {
    private int coachId;
    private ArrayList<HashMap<String, String>> mGroupData = null;
    private ArrayList<ArrayList<String>> mData = null;
    private View RootView;
    private PullToRefreshExpandableListView list;
    private TextView title;
    private ImageButton back;
    private String titlestr;
    private LoadingView loading; //?

    @Override
    public void onResume() {
        //??
        getWebData();
        super.onResume();

    }

    public CoachMealListFragment(Context context, String titlestr, String url, int coachId) {
        this.coachId = coachId;
        this.titlestr = titlestr;
        this.setTag("CoachMealListFragment");
    }

    public CoachMealListFragment(Context context, Object tag, String titlestr, String url, int coachId) {
        this.coachId = coachId;
        this.titlestr = titlestr;
        this.setTag("CoachMealListFragment");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        try {
            RootView = inflater.inflate(R.layout.fragment_meallist, null);
            registView();
            initView();
        } catch (Exception e) {
        }
        return RootView;

    }

    private void initView() {
        this.title.setText(titlestr);
        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                getActivity().finish();
            }

        });
        list.setMode(Mode.PULL_FROM_START);
        list.setOnRefreshListener(new OnRefreshListener<ExpandableListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ExpandableListView> refreshView) {
                getWebData();
            }
        });
        this.loading.setReLoadingListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                getWebData();
            }

        });
    }

    private void registView() {
        list = (PullToRefreshExpandableListView) RootView.findViewById(R.id.f_meallist);
        title = (TextView) RootView.findViewById(R.id.header_title);
        back = (ImageButton) RootView.findViewById(R.id.header_back);
        loading = (LoadingView) RootView.findViewById(R.id.titlelist_loading);

    }

    public void getWebData() {
        if (coachId == -1) {
            AppUtil.ShowShortToast(this.getActivity(), "??");
        }

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", coachId + "");
        NetUtil.requestStringData(SRL.Method.METHOD_GET_COACH_MEAL, TAG, map, new Response.Listener<String>() {

            @Override
            public void onResponse(String result) {
                Log.i(TAG, "list is null ? " + (list == null));
                list.onRefreshComplete();
                if (result == null || result.equals("")) {
                    AppUtil.ShowShortToast(getActivity(), "??");
                } else {
                    setList(result, list.getRefreshableView());
                }
            }

        }, new DefaultErrorListener(this.getActivity(), null, loading, list));
    }

    public void setList(String data, ExpandableListView list) {
        if (loading.getVisibility() == View.VISIBLE) {
            loading.setVisibility(View.GONE);
        }
        //
        mGroupData = new ArrayList<HashMap<String, String>>();
        mData = new ArrayList<ArrayList<String>>();
        try {
            //[{"carTypeContent":"","cash":50,"cohId":1,"content":"?",
            //            "createDate":"2015-03-01","id":1,"schoolId":1,"title":"","type":2},
            //         {"carTypeContent":"?","cash":50,"cohId":1,"content":"??","createDate":"2015-03-18",
            //         "id":2,"schoolId":1,"title":"?","type":2}]
            JSONArray array = new JSONArray(data);
            for (int i = 0; i < array.length(); i++) {
                JSONObject json = array.getJSONObject(i);
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("mealname", json.optString(SRL.ReturnField.COACH_MEAL_TITLE, ""));
                map.put("mealprize", json.optString(SRL.ReturnField.COACH_MEAL_PRIZE, ""));
                mGroupData.add(map);
                ArrayList<String> childmap = new ArrayList<String>();
                childmap.add("???"
                        + json.optString(SRL.ReturnField.COACH_MEAL_CONTENT, ""));
                childmap.add("" + json.optString(SRL.ReturnField.COACH_MEAL_CARTYPE, ""));
                childmap.add("?        " + json.optString(SRL.ReturnField.COACH_MEAL_CLASSTYPE, ""));
                childmap.add("" + json.optString(SRL.ReturnField.COACH_MEAL_LICENSETYPE, ""));
                mData.add(childmap);
            }

            CoachMealListExpandAdapter judgeAdapter = new CoachMealListExpandAdapter(this.getActivity(), mGroupData,
                    mData);
            list.setAdapter(judgeAdapter);
            if (mGroupData.size() > 0) {
                list.expandGroup(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}