0

My Radio_Cabinet_Defect_Activity:

public class Radio_Cabinet_Defect_Activity extends Activity {

    private ListView mList;
    private CabinetDefectAdapter mCabinetDefectAdapter;

    private RadioSiteSubsection mRadioSiteSubsection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_radio__cabinet__defect);

        Intent i = getIntent();
        if (i.hasExtra("radio_site_subsection_data")) {
            mRadioSiteSubsection = i.getParcelableExtra("radio_site_subsection_data");
        }

        mList = (ListView) findViewById(R.id.cabinet_defect_list);

        TextView title = (TextView) findViewById(R.id.cabinet_point_title);
        title.setText(mRadioSiteSubsection.getSubsectionNum() + " " + mRadioSiteSubsection.getName());

        TextView description = (TextView) findViewById(R.id.cabinet_point_description);
        description.setText(mRadioSiteSubsection.getDescription());

        CheckBox okCheckBox = (CheckBox) findViewById(R.id.checkbox_point_ok);
        okCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                ArrayList<Severity> severities = mRadioSiteSubsection.getGlobalSeverities();
                severities.get(SeverityList.OK.intValue()).setChecked(isChecked);

                // If we checked OK, then other checkboxes must be unchecked
                if (isChecked) {
                    for (int i = SeverityList.CRITICAL.intValue(); i < severities.size(); ++i) {
                        severities.get(i).setChecked(false);
                    }
                }

                mRadioSiteSubsection.setGlobalSeverities(severities);
            }
        });

        Button backBtn = (Button) findViewById(R.id.cabinet_point_back_btn);
        backBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    public void onResume() {
        super.onResume();

        mCabinetDefectAdapter = new CabinetDefectAdapter(Radio_Cabinet_Defect_Activity.this,
                mRadioSiteSubsection.getDefects());

        mList.setOnItemClickListener(null);
        mList.setAdapter(mCabinetDefectAdapter);

        mCabinetDefectAdapter.notifyDataSetChanged();
    }
}

I passed the object list to the following Adapter:

public class CabinetDefectAdapter extends ArrayAdapter<RadioSiteDefect> {

    private final Context mContext;

    static class ViewHolder {
        public TextView mText;
        public CheckBox mCabinetDefectChkBox;
    }

    public CabinetDefectAdapter(Context context, List<RadioSiteDefect> radio_site_defects) {
        super(context, R.layout.radio__cabinet_defect_item, radio_site_defects);
        this.mContext = context;
    }

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

        View rowView = convertView;

        // reuse views
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.radio__cabinet_defect_item, parent, false);

            // configure view holder
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.mText = (TextView) rowView.findViewById(R.id.cabinet_defect_details);
            viewHolder.mCabinetDefectChkBox = (CheckBox) rowView.findViewById(R.id.cabinet_defect_details_checkbox);

            rowView.setTag(viewHolder);
        }

        // fill data
        final ViewHolder holder = (ViewHolder) rowView.getTag();

        final RadioSiteDefect radio_site_defect = super.getItem(position);
        holder.mText.setText(radio_site_defect.getDefectNum() + " " + radio_site_defect.getDescription());
        holder.mCabinetDefectChkBox.setText(radio_site_defect.getSeverity().getName());

        if (radio_site_defect.getSeverity().isChecked()) {
            holder.mCabinetDefectChkBox.setChecked(true);
        } else {
            holder.mCabinetDefectChkBox.setChecked(false);
        }

        holder.mCabinetDefectChkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                radio_site_defect.getSeverity().setChecked(isChecked);
            }
        });

        return rowView;
    }
}

This adapter is linked with a ListView backed by the custom view R.layout.radio__cabinet_defect_item.

When I press on the checkbox, I update the corresponding checkbox flag on my object. But on Radio_Cabinet_Defect_Activity, the object remains unchanged. Is there a way to get the object modified, or am I completely missing the point?

Thanks in advance.

EDIT

I missed an important part. All starts from this Activity (Radio_Cabinet_Defect_Activity):

public class Radio_Cabinet_Activity extends Activity {

private ListView mList;
private CabinetAdapter mCabinetAdapter;

private RadioSite mCabinetData;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_radio__cabinet_);

    mList = (ListView) findViewById(R.id.cabinet_list);

    loadCabinetData();

    mCabinetAdapter = new CabinetAdapter(Radio_Cabinet_Activity.this, mCabinetData.getSubsections());

    mList.setOnItemClickListener(null);
    mList.setAdapter(mCabinetAdapter);

    Button backBtn = (Button) findViewById(R.id.cabinet_back_btn);
    backBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();
        }
    });
}

@Override
public void onBackPressed() {

}

private void loadCabinetData() {
    if (mCabinetData == null) {
        mCabinetData = new RadioSite();
    }

    String[] radio_site_list = getResources().getStringArray(R.array.radio_site_list);
    String[] radio_severity_list = getResources().getStringArray(R.array.radio_severity_list);

    mCabinetData.setId(RadioSiteList.CABINET.intValue());
    mCabinetData.setName(radio_site_list[RadioSiteList.CABINET.intValue()]);

    String[] cabinet_categories = getResources().getStringArray(R.array.cabinet_categories);
    String[] cabinet_categories_details = getResources().getStringArray(R.array.cabinet_categories_details);

    // Add subsections
    ArrayList<RadioSiteSubsection> cabinet_subsections = new ArrayList<RadioSiteSubsection>(
            cabinet_categories.length);
    for (int i = 0; i < cabinet_categories.length; ++i) {
        RadioSiteSubsection subsection = new RadioSiteSubsection();

        subsection.setSubsectionNum((RadioSiteList.CABINET.intValue() + 1) + "." + (i + 1));
        subsection.setName(cabinet_categories[i]);
        subsection.setDescription(cabinet_categories_details[i]);

        // Add severities
        ArrayList<Severity> global_severities = new ArrayList<Severity>(radio_severity_list.length);
        for (int j = 0; j < radio_severity_list.length; ++j) {
            Severity severity = new Severity();
            severity.setName(radio_severity_list[j]);

            global_severities.add(severity);
        }

        subsection.setGlobalSeverities(global_severities);

        // Add defects for this subsection
        ArrayList<RadioSiteDefect> cabinet_sub_defects = new ArrayList<RadioSiteDefect>();
        String[] defect_list = getResources().getStringArray(CommonResources.getCabinetDefectList(i));

        for (int j = 0; j < defect_list.length; ++j) {
            RadioSiteDefect defect = new RadioSiteDefect();
            defect.setDefectNum((RadioSiteList.CABINET.intValue() + 1) + "." + (i + 1) + "." + (j + 1));

            String[] defectStr = defect_list[j].split("\\|");
            defect.setDescription(defectStr[0]);

            Severity severity = new Severity();
            severity.setName(defectStr[1]);

            defect.setSeverity(severity);

            cabinet_sub_defects.add(defect);
        }

        subsection.setDefects(cabinet_sub_defects);

        cabinet_subsections.add(subsection);
    }

    mCabinetData.setSubsections(cabinet_subsections);
}

As you can see I have a subobject of RadioSite that I pass through the following Adapter:

public class CabinetAdapter extends ArrayAdapter<RadioSiteSubsection> {

    private final Context mContext;
    private final String[] mRadioSeverityList;

    static class ViewHolder {
        public TextView mText;
        public Button mCabinetBtn;
        public Button mCabinetNotesBtn;
        public Button mCabinetImgBtn;
        public CheckBox[] mCabinetChkBox;
    }

    public CabinetAdapter(Context context, List<RadioSiteSubsection> radio_site_subsections) {
        super(context, R.layout.radio__cabinet_item, radio_site_subsections);
        this.mContext = context;
        this.mRadioSeverityList = mContext.getResources().getStringArray(R.array.radio_severity_list);
    }

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

        View rowView = convertView;

        // reuse views
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.radio__cabinet_item, parent, false);

            // configure view holder
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.mText = (TextView) rowView.findViewById(R.id.cabinet_desc);
            viewHolder.mCabinetBtn = (Button) rowView.findViewById(R.id.cabinet_btn);
            viewHolder.mCabinetNotesBtn = (Button) rowView.findViewById(R.id.cabinet_btn_notes_obs);
            viewHolder.mCabinetImgBtn = (Button) rowView.findViewById(R.id.cabinet_btn_image);

            viewHolder.mCabinetChkBox = new CheckBox[CommonResources.getRadioCheckboxLength()];

            for (int i = 0; i < mRadioSeverityList.length; ++i) {
                viewHolder.mCabinetChkBox[i] = (CheckBox) rowView.findViewById(CommonResources.getRadioCheckboxRes(i));
            }

            rowView.setTag(viewHolder);
        }

        // fill data
        ViewHolder holder = (ViewHolder) rowView.getTag();

        final RadioSiteSubsection radio_site_subsection = super.getItem(position);
        holder.mText.setText(radio_site_subsection.getSubsectionNum() + " " + radio_site_subsection.getName());
        holder.mCabinetBtn.setText(radio_site_subsection.getSubsectionNum());


        holder.mCabinetBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, Radio_Cabinet_Defect_Activity.class);
                intent.putExtra("radio_site_subsection_data", radio_site_subsection);
                mContext.startActivity(intent);
            }
        });

        holder.mCabinetNotesBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, NotesObservationActivity.class);
                intent.putExtra("radio_site_subsection_data", radio_site_subsection);
                mContext.startActivity(intent);
            }
        });

        return rowView;
    }
}

As you can see, I pass the subobject RadioSiteSubsection of RadioSite to the Radio_Cabinet_Defect_Activity (the activity posted earlier). What I want, is that when I return to the Radio_Cabinet_Activity, I would like that RadioSite had updated values inside. I hope that I've explained well the situation.

EDIT 2

Thank you @Sufian. To re-cap I have the main activity that is Radio_Cabinet_Activity that has a RadioSite object.

RadioSite object has several subobjects. So I pass the RadioSiteSubsection subobject list of RadioSite to the CabinetAdapter to show it in Listview. Inside CabinetAdapter I have an OnClickListener in which I pass the single RadioSiteSubsection object to the next activity Radio_Cabinet_Defect_Activity. In this activity, I have another adapter Cabinet_Defect_Adapter where I pass a subobject list of RadioSiteSubsection, named RadioSiteDefect. In this last adapter, I have an OnCheckedChangeListener for checkboxes and when it's called, I update the corresponding field on RadioSiteDefect object. The desired final situation is to have the original RadioSite object modified when I return back to Radio_Cabinet_Activity.

I know that is a little twisted :)

Manugal
  • 15
  • 1
  • 7

1 Answers1

0

The problem wasn't related to how to get checkboxes values back, but how to get updated values from the checkboxes in a ListView and save them on my object (and in turn transfer that updated object to previous activity).

I solved by calling getItem() method of ListView, so I was able to get updated elements.

Thanks for support.

Manugal
  • 15
  • 1
  • 7
  • Would've been better if you gave the source which was [this answer](http://stackoverflow.com/a/27035785/1276636). – Sufian Mar 09 '15 at 09:45