0
當我第一次運行我的應用時,我的ListView
中的CheckBox
項未選中。在我的layout.xml
我有一個CheckBox
以上我的ListView
項目。當檢測到我的ListView
以上的單個CheckBox
被選中時,它應該將我的ListView
中的所有CheckBox
項標記爲選中狀態。我的下面的代碼不會更新我的ListView
中CheckBox
項的顯示,但當我試圖通過調用checkItem.isChecked()
來獲取我ListView
中每個CheckBox
項的布爾值並讓它顯示在我的logcat
中時,它將返回true。我錯過了什麼?如何更新我ListView中CheckBox項的顯示標記爲check?
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()
}