如何更新我的ListView中标记为check的CheckBox项目的显示?

时间:2013-09-26 05:08:05

标签: android android-layout android-listview android-adapter android-checkbox

首次运行我的应用时,我的CheckBox中的ListView项目未经检查。在我的layout.xml中,我的CheckBox项上方有一个ListView。如果选中CheckBox之上的ListView,则应将CheckBox项中的所有ListView项标记为已选中。我的代码不会更新我CheckBox中我ListView项目的显示,但是当我尝试通过调用我的CheckBox中的每个ListView项目获取布尔值时checkItem.isChecked()并让它显示在我的logcat中,它返回true。我错过了什么吗?

package com.usjr.sss.fragment;

import java.util.ArrayList;
import com.usjr.sss.R;
import com.usjr.sss.activity.CourseFragmentActivity;
import com.usjr.sss.adapter.InfoTechAdapter;
import com.usjr.sss.adapter.SubjectDbAdapter;
import android.database.Cursor;
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.CheckBox;
import android.widget.ListView;
import android.widget.Toast;

public class InfoTechFirstYearFragment extends Fragment {

private CheckBox checkFirstYearFirstSem;
private ListView listItFirstYearFirstSem;
private ListView listItFirstYearSecondSem;
private InfoTechAdapter infoTechAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_it_first_year, container,
            false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.i("InfoTechFirstYearFragment", "onActivityCreated");

    listItFirstYearFirstSem = (ListView) getActivity().findViewById(
            R.id.listItFirstYearFirstSem);
    listItFirstYearSecondSem = (ListView) getActivity().findViewById(
            R.id.listItFirstYearSecondSem);
}

@Override
public void onStart() {
    super.onStart();
    Log.i("InfoTechFirstYearFragment", "onStart");

    SubjectDbAdapter subjectDbAdapter = new SubjectDbAdapter(getActivity());
    subjectDbAdapter.open();

    Cursor cursor = subjectDbAdapter.fetchAllSubjects();
    ArrayList<String> arrayListSubject = new ArrayList<String>();
    ArrayList<String> arrayListFirstSem = new ArrayList<String>();
    ArrayList<String> arrayListSecondSem = new ArrayList<String>();

    while (cursor.moveToNext()) {
        String subject = cursor.getString(cursor
                .getColumnIndexOrThrow(SubjectDbAdapter.SUBJECT_ID));
        arrayListSubject.add(subject);
    }// end while

    subjectDbAdapter.close();

    int index;

    /**
     * 1st yr 1st sem
     */
    for (index = 0; index < 9; index++) {
        arrayListFirstSem.add(arrayListSubject.get(index));
    }

    infoTechAdapter = new InfoTechAdapter(
            (CourseFragmentActivity) getActivity(), arrayListFirstSem);
    listItFirstYearFirstSem.setAdapter(infoTechAdapter);

    /**
     * 1st yr 2nd sem
     */
    for (index = 9; index < 18; index++) {
        arrayListSecondSem.add(arrayListSubject.get(index));
    }

    infoTechAdapter = new InfoTechAdapter(
            (CourseFragmentActivity) getActivity(), arrayListSecondSem);
    listItFirstYearSecondSem.setAdapter(infoTechAdapter);

    /**
     * MARK ALL THE CHECKBOX AS CHECKED
     */
    checkFirstYearFirstSem = (CheckBox) getActivity().findViewById(
            R.id.checkFirstYearFirstSem);
    checkFirstYearFirstSem.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkFirstYearFirstSem.isChecked()) {
                Toast.makeText(getActivity(), "ckecked", Toast.LENGTH_SHORT)
                        .show();
                int count = listItFirstYearFirstSem.getCount();
                for (int index = 0; index < count; index++) {

                    // View viewMy =
                    // listItFirstYearFirstSem.getChildAt(index);
                    // Object id = v.getTag();

                    View viewItem = (View) listItFirstYearFirstSem
                            .getAdapter().getView(index, getView(),
                                    listItFirstYearFirstSem);
                    CheckBox checkItem = (CheckBox) viewItem
                            .findViewById(R.id.subject);
                    checkItem.setChecked(true);

                    listItFirstYearFirstSem.setItemChecked(index, true);

                    infoTechAdapter.notifyDataSetChanged();
                    Log.i("checkItem",
                            String.valueOf(checkItem.getText().toString()));
                    Log.i("checkItem",
                            String.valueOf(checkItem.isChecked()));
                }

            } else {
                Toast.makeText(getActivity(), "unckecked",
                        Toast.LENGTH_SHORT).show();
            }// end if-else(checkFirstYearFirstSem.isChecked())
        }// end onClick
    });// end OnClickListener
}// end onStart()
}

1 个答案:

答案 0 :(得分:0)

我自己解决了!

而不是使用

View viewItem = (View) listItFirstYearFirstSem.getAdapter().getView(index, getView(), listItFirstYearFirstSem);

我用过

View viewItem = listItFirstYearFirstSem.getChildAt(index);

然后

CheckBox checkItem = (CheckBox) viewMy.findViewById(R.id.subject);
checkItem.setChecked(true);