我正在自定义ListView中从firebase数据库接收数据。现在,我要为该ListView添加一个复选框。我已将复选框添加到列表视图中,但它给了我一个错误。所以请任何人帮助我。这是我的代码。
public class AttendanceList extends ArrayAdapter<AttendanceSheetItems> {
private Activity context;
private List<AttendanceSheetItems> attendanceList;
public AttendanceList (Activity context,List<AttendanceSheetItems> attendanceList){
super(context,R.layout.attendance_sheet,attendanceList);
this.context=context;
this.attendanceList=attendanceList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
assert vi != null;
convertView = vi.inflate(R.layout.attendance_sheet, null);
holder = new ViewHolder();
holder.rollno = (TextView) convertView.findViewById(R.id.textViewroll);
holder.name1 = (TextView) convertView.findViewById(R.id.textViewName);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
AttendanceSheetItems attendanceSheetItems = (AttendanceSheetItems) cb.getTag();
Toast.makeText(context,
"Clicked on Checkbox: " + cb.getText() +
" is " + cb.isChecked(),
Toast.LENGTH_LONG).show();
attendanceSheetItems.setChecked(cb.isChecked());
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
AttendanceSheetItems attendanceSheetItems = attendanceList.get(position);
holder.rollno.setText( attendanceSheetItems.getRollno() );
holder.name1.setText(attendanceSheetItems.getName());
holder.name.setChecked(attendanceSheetItems.getChecked());
holder.name.setTag(attendanceSheetItems);
return convertView;
这是我的模型课。
public class AttendanceSheetItems {
String rollno;
String name;
Boolean Checked=false;
public AttendanceSheetItems(String rollno, String name, Boolean checked) {
this.rollno = rollno;
this.name = name;
Checked = checked;
}
public String getRollno() {
return rollno;
}
public void setRollno(String rollno) {
this.rollno = rollno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public void setChecked(Boolean checked) {
Checked = checked;}
public Boolean getChecked() {
return Checked; }
这里我正在从Firebase数据库接收数据。
public class TakeAttendance extends Activity {
Spinner clas;
Button ok,submit;
EditText date;
ListView lv;
DatabaseReference reff;
List<AttendanceSheetItems> attendanceList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_attendance);
clas = (Spinner) findViewById(R.id.sclas);
ok = (Button) findViewById(R.id.ok);
submit=(Button)findViewById(R.id.submit);
date=(EditText)findViewById(R.id.date);
lv = (ListView) findViewById(R.id.lvv);
attendanceList = new ArrayList<>();
reff = FirebaseDatabase.getInstance().getReference().child("Students");
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String searchText = clas.getSelectedItem().toString();
final Query firebaseSearchQuery = reff.orderByChild("clas").equalTo(searchText);
firebaseSearchQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot){
attendanceList.clear();
for (DataSnapshot artistSnapshot : dataSnapshot.getChildren()) {
AttendanceSheetItems attendanceSheetItems = artistSnapshot.getValue(AttendanceSheetItems.class);
attendanceList.add(attendanceSheetItems);
}
AttendanceList adapter = new AttendanceList(TakeAttendance.this, attendanceList);
lv.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}}
);}}
这是堆栈跟踪。
11-09 14:32:28.717 18321-18321/com.example.sanakhan.attendapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sanakhan.attendapp, PID: 18321
com.google.firebase.database.DatabaseException: Found two getters or fields with conflicting case sensitivity for property: checked
at com.google.android.gms.internal.firebase_database.zzku.zzae(Unknown Source:48)
at com.google.android.gms.internal.firebase_database.zzku.<init>(Unknown Source:303)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source:12)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source:180)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source:0)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
at com.example.sanakhan.attendapp.TakeAttendance$1$1.onDataChange(TakeAttendance.java:53)
at com.google.firebase.database.zzp.onDataChange(Unknown Source:7)
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source:13)
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source:2)
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source:71)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6600)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:821)
答案 0 :(得分:0)
尝试修改命名约定,遵循Java命名约定,将属性Boolean Checked=false
小写:Boolean Checked=false
,将getter和setter设为getChecked()
setChecked()
< / p>
我希望这对您有帮助