0

当我第一次运行我的应用程序时,CheckBox我的项目ListView未选中。在我的物品上面layout.xml有一个。当我上面的那个单被选中时,它应该将my 中的所有项目标记为已选中。我下面的代码不会更新我的项目在我的检查中的显示,但是当我尝试通过调用来获取我的每个项目的布尔值并让它显示在我的中时,它返回 true。我错过了什么?CheckBoxListViewCheckBoxListViewCheckBoxListViewCheckBoxListViewCheckBoxListViewcheckItem.isChecked()logcat

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()
}
4

1 回答 1

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);
于 2013-09-26T05:49:36.460 回答