CertificationCourseDetailAdapter.java 6.8 KB
package com.hh.xuetubao.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hh.xuetubao.MyServer;
import com.hh.xuetubao.R;
import com.hh.xuetubao.Utils.BaseObsever;
import com.hh.xuetubao.Utils.HttpUtils;
import com.hh.xuetubao.Utils.ListviewUtils;
import com.hh.xuetubao.Utils.NetConfig;
import com.hh.xuetubao.Utils.SharedPrefrenceUtils;
import com.hh.xuetubao.activity.pratice.PriacticeDistActivity;
import com.hh.xuetubao.bean.AnswerBean;
import com.hh.xuetubao.bean.DoExerciseBean;
import com.hh.xuetubao.bean.FreeCourseBean;
import com.hh.xuetubao.fragment.CurriculumScheduleFragment;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class CertificationCourseDetailAdapter extends BaseAdapter {
    private List<DoExerciseBean.bean> list;
    private Context context;
    private FragmentTransaction transaction;
    private CertificationCourseContentAdapter adapter;
    private int thisPosition;
    private int viewId;
    private int type;
    private List<String> cataloglst = new ArrayList<>();

    public CertificationCourseDetailAdapter(List<DoExerciseBean.bean> list, Context context, FragmentTransaction transaction, int viewId, int type) {
        this.list = list;
        this.context = context;
        this.transaction = transaction;
        this.viewId = viewId;
        this.type = type;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        thisPosition = position;
        View view = LayoutInflater.from(context).inflate(R.layout.lv_course_title, null);


        ImageView img = (ImageView) view.findViewById(R.id.img);
        TextView title = (TextView) view.findViewById(R.id.title);
        ListView lv = (ListView) view.findViewById(R.id.lv_content);

        List<DoExerciseBean.bean> catalogLst = list.get(position).getSubCatalogLst().get(position).getSubCatalogLst();
        adapter = new CertificationCourseContentAdapter(catalogLst, context);
        lv.setAdapter(adapter);
        ListviewUtils.setListViewHeightBasedOnChildren(lv);

        for (DoExerciseBean.bean a : catalogLst) {
            cataloglst.add(a.getCatalogOid());
        }

        SharedPrefrenceUtils.putStringList(context, "CataLogLst", cataloglst);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            private Intent intent;

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String oid = list.get(thisPosition).getSubCatalogLst().get(thisPosition).getSubCatalogLst().get(position).getCatalogOid();
                String accountOid = SharedPrefrenceUtils.getString(context, "AccountOid");
                if (type == 1) {
                    //认证课程
                    doRequestOne(accountOid, oid);
                } else if (type == 2) {
                    //练习
                    String title = list.get(thisPosition).getSubCatalogLst().get(thisPosition).getSubCatalogLst().get(position).getCatalogName();
                    doRequestTwo(oid, accountOid, title);
                }
            }
        });

        if (list.get(position).getSubCatalogLst().get(position).getCatalogName() != null)
            title.setText(list.get(position).getSubCatalogLst().get(position).getCatalogName());
        if (list.get(position).getCatalogPicUrl() != null && list.get(position).getCatalogPicUrl() != "")
            Glide.with(context).load(list.get(position).getCatalogPicUrl()).into(img);

        return view;
    }

    private void doRequestOne(String accountOid, final String oid) {
        MyServer mServers = HttpUtils.getInstance().getServer(NetConfig.learnUrl);
        mServers.getLessonLstByCatalogOid(accountOid, oid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObsever() {
                    @Override
                    public void onNext(Object value) {
                        super.onNext(value);
                        FreeCourseBean courseBean = (FreeCourseBean) value;
                        if (courseBean.getSuccess()) {
                            CurriculumScheduleFragment curriculumScheduleFragment = new CurriculumScheduleFragment();
                            transaction.replace(R.id.frame, curriculumScheduleFragment, oid);
                            transaction.commit();
                        } else {
                            Toast.makeText(context, "当前账号无权限,请拨打电话xxxxxxxxxx", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    private void doRequestTwo(final String catalogOid, String accountOid, final String title) {
        MyServer mServers = HttpUtils.getInstance().getServer(NetConfig.shuaTiUrl);
        mServers.GetRandomQuestions(catalogOid, Integer.valueOf(10), accountOid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObsever() {
                    @Override
                    public void onNext(Object value) {
                        super.onNext(value);
                        AnswerBean bean = (AnswerBean) value;
                        if (bean.isIsSuccess()) {
                            Intent intent = new Intent(context, PriacticeDistActivity.class);
                            intent.putExtra("CatalogOid", catalogOid);
                            SharedPrefrenceUtils.saveString(context, "PraticeTitle", title);
                            context.startActivity(intent);
                        } else {
                            Toast.makeText(context, "当前账号无权限,请拨打电话xxxxxxxxxx", Toast.LENGTH_SHORT).show();
                        }
                    }

                });

    }


}