如何在android中的自定义适配器中使用自定义适配器

时间:2015-08-20 13:32:03

标签: android

主适配器

public class NewTreatmentAdapter extends BaseAdapter {

    Context mContext;
    LayoutInflater inflater;
    ArrayList<MyTreatmentDetailNew> list;

    public NewTreatmentAdapter(Context context, ArrayList<MyTreatmentDetailNew> list) {
        this.mContext = context;
        this.list = list;

    }

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

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

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

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

        MyTreatmentDetailNew allergiesDetails = (MyTreatmentDetailNew) list.get(position);

        Holders holders;
        View view = convertView;

        if (convertView == null) {
            inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.new_my_treat_list, parent, false);

            holders = new Holders();
            holders.txtFrom = (TextView) view.findViewById(R.id.txtFrom);
            holders.txtTreatmnt = (TextView) view.findViewById(R.id.txtTreatmnt);
            holders.txtStatus = (TextView) view.findViewById(R.id.txtStatus);

            holders.lView = (ListView) view.findViewById(R.id.lView);


            holders.txtFrom.setTag(list.get(position));
            holders.txtTreatmnt.setTag(list.get(position));
            holders.txtStatus.setTag(list.get(position));
            holders.lView.setTag(list.get(position));

            view.setTag(holders);


        } else {
            holders = (Holders) view.getTag();

            ((Holders) view.getTag()).txtFrom.setTag(list.get(position));
            ((Holders) view.getTag()).txtTreatmnt.setTag(list.get(position));
            ((Holders) view.getTag()).txtStatus.setTag(list.get(position));
            ((Holders) view.getTag()).lView.setTag(list.get(position));
        }

        holders.txtFrom.setText(allergiesDetails.fromName);
        holders.txtTreatmnt.setText(allergiesDetails.treatmntName);
        holders.txtStatus.setText(allergiesDetails.status);

        ArrayList<MyTreatmentSubDetailNew> subDetailList = allergiesDetails.subDetailList;
        if(subDetailList!=null && subDetailList.size()!=0) {
            NewTreatmentSubAdapter subAdapter = new NewTreatmentSubAdapter(mContext, subDetailList);
            holders.lView.setAdapter(subAdapter);
            //((ViewGroup)convertView).addView(holders.lView);
            //holders.lView.setTag(allergiesDetails);
        }


        return view;
    }


    public class Holders {
        TextView txtFrom, txtTreatmnt, txtStatus, txtDay, txtMg, txtOften, txtName;
        ListView lView;
    } }

SubAdapter

public class NewTreatmentSubAdapter extends BaseAdapter {

    Context mContext;
    LayoutInflater inflater;
    ArrayList<MyTreatmentSubDetailNew> list;

    public NewTreatmentSubAdapter(Context context, ArrayList<MyTreatmentSubDetailNew> list) {
        this.mContext = context;
        this.list = list;

    }

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

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

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

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

        MyTreatmentSubDetailNew allergiesDetails = (MyTreatmentSubDetailNew) list.get(position);
        System.out.println("sub position >> "+position);

        Holders holders;
        View view = convertView;

        if (convertView == null) {
            inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           // view = inflater.inflate(R.layout.new_my_treat_sub_list, parent, false);
            view =  inflater.inflate(R.layout.new_my_treat_sub_list, null);

            holders = new Holders();
            holders.txtDay = (TextView) view.findViewById(R.id.txtDay);
            holders.txtMg = (TextView) view.findViewById(R.id.txtMg);
            holders.txtOften = (TextView) view.findViewById(R.id.txtOften);
            holders.txtName = (TextView) view.findViewById(R.id.txtName);

            holders.txtDay.setTag(list.get(position));
            holders.txtMg.setTag(list.get(position));
            holders.txtOften.setTag(list.get(position));
            holders.txtName.setTag(list.get(position));

            view.setTag(holders);


        } else {
            holders = (Holders) view.getTag();

            ((Holders) view.getTag()).txtDay.setTag(list.get(position));
            ((Holders) view.getTag()).txtMg.setTag(list.get(position));
            ((Holders) view.getTag()).txtName.setTag(list.get(position));
            ((Holders) view.getTag()).txtOften.setTag(list.get(position));
        }

        holders.txtDay = (TextView) view.findViewById(R.id.txtDay);
        holders.txtMg = (TextView) view.findViewById(R.id.txtMg);
        holders.txtOften = (TextView) view.findViewById(R.id.txtOften);
        holders.txtName = (TextView) view.findViewById(R.id.txtName);

        holders.txtDay.setText("" + allergiesDetails.Days);
        holders.txtMg.setText(allergiesDetails.Dosage);
        holders.txtName.setText(allergiesDetails.MedicineName);
        holders.txtOften.setText(allergiesDetails.AsNeed + "," + allergiesDetails.Morning + "," + allergiesDetails.AfterNoon + "," + allergiesDetails.Night);

       // ((ViewHolder) view.getTag()).edittextComments.setTag(arrayListIndividualEvaluationSectionsService.get(position));


        return view;
    }


    public class Holders {
        TextView txtFrom, txtTreatmnt, txtStatus, txtDay, txtMg, txtOften, txtName;
        ListView lView;
    }

/*
    public Object getItem(int position) {
        for (Organization org : orgnizations) {
            if (position == 0) {
                return org;
            }
            position--;
            List<Contact> contacts = org.getContacts();
            if (contacts != null) {
                int size = contacts.size();
                if (position >= size) {
                    position -= size;
                } else {
                    return contacts.get(position);
                }
            }
        }*/
}

0 个答案:

没有答案