Android - 无法从ListView获取对象修改

时间:2015-02-24 22:16:46

标签: android android-listview android-arrayadapter parcelable

我的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();
    }
}

我将对象列表传递给以下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;
    }
}

此适配器与自定义视图R.layout.radio__cabinet_defect_item支持的ListView链接。

当我按下复选框时,我会更新对象上的相应复选框标记。但是在Radio_Cabinet_Defect_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);
}

正如您所看到的,我有RadioSite的子对象,我通过以下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;
    }
}

如您所见,我将RadioSiteSubsection的子对象RadioSite传递给Radio_Cabinet_Defect_Activity(之前发布的活动)。我想要的是,当我回到Radio_Cabinet_Activity时,我希望RadioSite内部更新了值。我希望我已经很好地解释了这种情况。

编辑2

谢谢@Sufian。要重新设置上限,我的主要活动是Radio_Cabinet_Activity,其中包含RadioSite个对象。

RadioSite对象有几个子对象。因此,我将RadioSiteSubsection的{​​{1}}子对象列表传递给RadioSite,以便在CabinetAdapter中显示它。在Listview内,我有一个CabinetAdapter,其中我将单个OnClickListener对象传递给下一个活动RadioSiteSubsection。在此活动中,我有另一个适配器Radio_Cabinet_Defect_Activity,我在其中传递Cabinet_Defect_Adapter的子对象列表,名为RadioSiteSubsection。在最后一个适配器中,我有一个RadioSiteDefect复选框,当它被调用时,我更新OnCheckedChangeListener对象上的相应字段。期望的最终情况是在返回RadioSiteDefect时修改原始RadioSite对象。

我知道这有点扭曲:)

1 个答案:

答案 0 :(得分:0)

问题与如何获取复选框值无关,但如何从ListView中的复选框中获取更新值并将其保存在我的对象上(并将该更新后的对象转移到以前的活动) )。

我通过调用getItem()的{​​{1}}方法解决了问题,因此我能够获得更新的元素。

感谢您的支持。