I'm having trouble getting my checkboxes checked when any of the functions(which are commented out now) are called inside my listView onItemClickedListener. toggleChecked should only update my database and RefreshList is supposed to update my listView. My onItemClickListener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ListItemDetails listItemDetails = (ListItemDetails) arrayAdapter.getItem(i);
//toggleChecked(i, view);
//RefreshList(arrayAdapter);
CheckBox checkBox = (CheckBox)view.findViewById(R.id.listitems_checkbox);
checkBox.setChecked(!checkBox.isChecked());
}
});
toggleChecked function
public void toggleChecked(final Integer i,final View view)
{
final DatabaseReference RootRef;
RootRef = FirebaseDatabase.getInstance().getReference();
RootRef.child("Lists").child(ListDetails.getID()).child("listitems").child(arrayList.get(i).getID()).child("checked").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String checked = snapshot.getValue(String.class);
Map<String, Object> map = new HashMap<>();
map.put("checked",(checked.equals("0") ? "1" : "0"));
RootRef.child("Lists").child(ListDetails.getID()).child("listitems").child(arrayList.get(i).getID()).updateChildren(map);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
RefreshList function
public void RefreshList(final ArrayAdapter<ListItemDetails> arrayAdapter) {
DatabaseReference reference;
reference=FirebaseDatabase.getInstance().getReference().child("Lists");
reference.child(ListDetails.getID()).child("listitems").orderByChild("name").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
arrayList.clear();
for(DataSnapshot ds : snapshot.getChildren())
{
ListItemDetails temp = new ListItemDetails();
String name = ds.child("name").getValue(String.class);
temp.setName(name);
String ID = ds.child("ID").getValue(String.class);
temp.setID(ID);
String checked = ds.child("checked").getValue(String.class);
temp.setChecked(checked);
arrayList.add(temp);
}
listView.setAdapter(arrayAdapter);
for(int i=0; i<listView.getAdapter().getCount(); i++)
{
View v = getViewByPosition(i,listView);
CheckBox checkBox = (CheckBox)v.findViewById(R.id.listitems_checkbox);
if(arrayList.get(i).getChecked().equals("1"))
{
checkBox.setChecked(true);
}
else {
checkBox.setChecked(false);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
XML for the adapter
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="35sp"
android:id="@+id/textt">
</TextView>
<CheckBox
android:id="@+id/listitems_checkbox"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentEnd="true"
android:clickable="false"
/>
</RelativeLayout>
Adapter is declared like this
final ArrayAdapter arrayAdapter = new ArrayAdapter<ListItemDetails>(this, R.layout.list_items_checkbox, R.id.textt, arrayList);
Aucun commentaire:
Enregistrer un commentaire