我可以将数据从数据库加载到我的自定义ListView中,但是当我按下按钮时,在选中复选框(可以检查多个复选框)时,无法显示项目(相关,courseName是精确的)。
这是我的适配器代码:
public class CourseSelectionAdapter extends ArrayAdapter<CourseDisplay>{
private Context mContext;
private LayoutInflater mInflater;
int layoutResourceId;
public static List<CourseDisplay> mDataSource;
public static int checkBoxCounter;
public CourseSelectionAdapter(Context context, int layoutResourceId, List<CourseDisplay> items) {
super(context, layoutResourceId, items);
mContext = context;
mDataSource = items;
this.layoutResourceId = layoutResourceId;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
checkBoxCounter = 0;
}
@Override
public int getCount() {
return mDataSource.size();
}
@Override
public CourseDisplay getItem(int position) {
return mDataSource.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
//4
@Override
public View getView(int position, final View convertView, ViewGroup parent) {
// Get view for row item
View row = convertView;
ViewHolder holder;
holder = null;
CourseDisplay courseDisplay = getCourse(position);
// 1
if (row == null) {
// 2
row = mInflater.inflate(R.layout.custom_listview_layout, parent, false);
// 3
holder = new ViewHolder();
holder.courseCodeTextView = (TextView) row.findViewById(R.id.course_id_display);
holder.courseNameTextView = (TextView) row.findViewById(R.id.course_name_display);
holder.courseSelectedCheckBox = (CheckBox) row.findViewById(R.id.course_selection_checkbox);
holder.courseCodeTextView.setText(courseDisplay.getCourseCode());
holder.courseNameTextView.setText(courseDisplay.getCourseName());
holder.courseSelectedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
checkBoxCounter++;
getCourse((Integer) buttonView.getTag()).getCourseSelected();
}else {
checkBoxCounter--;
}
System.out.println("Count is: "+ checkBoxCounter);
}
});
// 4
row.setTag(holder);
} else {
// 5
holder = (ViewHolder) row.getTag();
}
// 6
holder.courseSelectedCheckBox.setTag(position);
holder.courseSelectedCheckBox.setChecked(courseDisplay.getCourseSelected());
return row;
}
private static final class ViewHolder {
public TextView courseCodeTextView;
public TextView courseNameTextView;
public CheckBox courseSelectedCheckBox;
}
CourseDisplay getCourse(int position) {
return ((CourseDisplay) getItem(position));
}
List<CourseDisplay> getBox() {
List<CourseDisplay> box = new ArrayList<CourseDisplay>();
for (CourseDisplay p : mDataSource) {
if (p.getCourseSelected())
box.add(p);
}
return box;
}
}//end class
这是我的CourseDisplay代码:
package com.example.android.shustudenthelper;
import static android.R.attr.checked;
public class CourseDisplay{
private String courseCode;
private String courseName;
private boolean courseSelected;
public CourseDisplay(String courseCode, String courseName, boolean courseSelected) {
this.courseCode = courseCode;
this.courseName = courseName;
this.courseSelected = courseSelected;
}
public String getCourseCode() {
return courseCode;
}
public String getCourseName() {
return courseName;
}
public boolean getCourseSelected(){
return courseSelected;
}
}//end class
这是我从数据库中获取数据的数据库方法:
public List<CourseDisplay> getAllCoursesForRegistration() {
List<CourseDisplay> courses = new ArrayList<>();
openDataBase();
Cursor mCursor = database.query(TABLE_COURSES, new String[] {COLUMN_COURSE_CODE,COLUMN_COURSE_NAME,COLUMN_COURSE_SELECTED,COLUMN_COURSE_SELECTED }, null, null, null, null, null);
if(mCursor.moveToFirst()){
do{
String courseCode = mCursor.getString(0);
String courseName = mCursor.getString(1);
boolean courseSelected = Boolean.parseBoolean(mCursor.getString(2));
courses.add(new CourseDisplay(courseCode, courseName,courseSelected));
}while(mCursor.moveToNext());
}
mCursor.close();
close();//close Database
return courses;
}//end method
这是我的主要活动:
public class CourseSelectionActivity extends AppCompatActivity {
public static Button confirm_Button;
public List<String> labels;
private static CheckBox saveCourseCheckBox;
public int checkAccumulator = 0;
private ListView myListView;
CourseSelectionAdapter newAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_selection);
DatabaseOpenHelper myDbHelper = new DatabaseOpenHelper(this);
try {
// check if database exists in app path, if not copy it from assets
myDbHelper.create();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
// open the database
myDbHelper.openDataBase();
myDbHelper.getWritableDatabase();
} catch (SQLException sqle) {
throw sqle;
}
//Display in ListView
populateListView();
// close the database
myDbHelper.close();
onClickConfirmButtonListener();
}//end OnCreate
private void populateListView(){
DatabaseOpenHelper myDbHelper = new DatabaseOpenHelper(this);//New Object of Database
List<CourseDisplay> courseDisplay_data = new ArrayList<CourseDisplay>();
courseDisplay_data = myDbHelper.getAllCoursesForRegistration();
newAdapter = new CourseSelectionAdapter(this,R.layout.custom_listview_layout,courseDisplay_data);
myListView = (ListView)findViewById(R.id.listview_courses);
myListView.setChoiceMode(2);
myListView.setAdapter(newAdapter);
}
public void onClickConfirmButtonListener() {
confirm_Button = (Button) findViewById(R.id.course_register_button);
confirm_Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!(checkBoxCounter < 1) && checkBoxCounter <= 4) {
showResult();//Show the courses selected.
Toast.makeText(CourseSelectionActivity.this, "You are directed to Notifications", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("com.example.android.shustudenthelper.NotificationSetupActivity");
startActivity(intent);}
else if (checkBoxCounter > 4){
Toast.makeText(CourseSelectionActivity.this, "You cannot select more than 4 courses", Toast.LENGTH_SHORT).show();
}else if (checkBoxCounter < 1){
Toast.makeText(CourseSelectionActivity.this, "You need to select at least one course", Toast.LENGTH_SHORT).show();
}
}
});
}
public void showResult() {
String result = "Selected Courses are :";
for (CourseDisplay p : newAdapter.getBox()) {
if (p.getCourseSelected()){
result += "\n" + p.getCourseName();
}
}
Toast.makeText(this, result+"\n",Toast.LENGTH_LONG).show();
}
}//end class
请指导我完成这个。
答案 0 :(得分:0)
首先,在CourseDisplay类中添加一个setter方法
public boolean setCourseSelected(boolean value){
this.courseSelected = value;
}
在CourseSelectionAdapter类中,单击复选框时,使用上面的方法设置复选框的值
holder.courseSelectedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
checkBoxCounter++;
courseDisplay.setCourseSelected(isChecked);
}else {
checkBoxCounter--;
}
System.out.println("Count is: "+ checkBoxCounter);
}
});
最后,像这样改变showResult()
public void showResult() {
String result = "Selected Courses are :";
for (CourseDisplay p : courseDisplay_data) {
if (p.getCourseSelected()){
result += "\n" + p.getCourseName();
}
}
Toast.makeText(this, result+"\n",Toast.LENGTH_LONG).show();
}