I'm making a shopping list App in android with firebase, I obtain the list of items , and show in array adapter well.When I need to check an item and change it in Firebase . I don't know if I have to do that in the array adapter or in my fragment. My class is itemCart and extends from another Food that have an ID. My problem is when I check the item, and i can't acces to the object that contains this checkbox, how can I do ?
I have this class:
public class itemCart extends Food implements Serializable {
public String quantity;
public String mesurementunit;
public Boolean bought;
public itemCart(){}
public itemCart(Food food, String quantity, String mesurementunit, Boolean bought) {
super(food.get_id());
this.quantity = quantity;
this.mesurementunit = mesurementunit;
this.bought = bought;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public String getMesurementunit() {
return mesurementunit;
}
public void setMesurementunit(String mesurementunit) {
this.mesurementunit = mesurementunit;
}
public Boolean getBought() {
return bought;
}
public void setBought(Boolean bought) {
this.bought = bought;
}
}
Also I have this ArrayAdapter
public class ShoppingListFragmentArrayAdapter extends ArrayAdapter<itemCart> {
List<itemCart> listItemsCart;
itemCart mitemCart;
FirebaseDatabase mFirebaseDatabase;
String uid;
FirebaseAuth mFirebaseAuth;
DatabaseReference mitemCardReference;
public ShoppingListFragmentArrayAdapter(@NonNull Context context, int resource, List<itemCart> objects) {
super(context, resource, objects);
this.listItemsCart = objects;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null){
convertView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.row_shopping_cart,parent,false);
}
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
uid = mFirebaseAuth.getUid().toString();
mitemCardReference = mFirebaseDatabase.getReference().child("carts").child(uid);
TextView foodName = (TextView) convertView.findViewById(R.id.txtViewNameShoppingList);
CircleImageView imgFood = (CircleImageView) convertView.findViewById(R.id.foodCardCircleView);
CheckBox checkBoxFood = (CheckBox) convertView.findViewById(R.id.chartFoodChecklist);
mitemCart = listItemsCart.get(position);
checkBoxFood.setChecked(mitemCart.getBought());
foodName.setText(mitemCart.getCorrectLanguageName()+" "+mitemCart.getQuantity()+" "+mitemCart.getMesurementunit());
Glide.with(imgFood.getContext())
.load(mitemCart.getImg_firebase())
.into(imgFood);
checkBoxFood.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//here i wanna get the object
}
});
return convertView;
}
}
Is correct to do this in the Adapter or I have to do in my Fragment ?
public class ShoppingListFragment extends Fragment {
private View rootView;
private ShoppingListFragmentArrayAdapter mshoppingListFragmentArrayAdapter;
private ProgressBar mProgressBar;
private CheckBox mcheckBox;
private Button mBtnClear;
private String uid;
List<itemCart> itemCarts;
private itemCart mitemCart;
private ListView mListView;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mItemCartReference;
public ShoppingListFragment (){
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uid = FirebaseAuth.getInstance().getCurrentUser().getUid().toString();
mFirebaseDatabase = FirebaseDatabase.getInstance();
mItemCartReference = mFirebaseDatabase.getReference().child("carts").child(uid);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_shopping_card_list,container,false);
mListView = (ListView) rootView.findViewById(R.id.lstViewShoppingCart);
mProgressBar = (ProgressBar) rootView.findViewById(R.id.progressBarCartFoodList);
mBtnClear = (Button) rootView.findViewById(R.id.btnClearShoppingList);
mcheckBox = (CheckBox) rootView.findViewById(R.id.chartFoodChecklist);
itemCarts = new ArrayList<>();
mProgressBar.setVisibility(View.VISIBLE);
mItemCartReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
clearItemCartList();
for (DataSnapshot itemSnapshot : dataSnapshot.getChildren()) {
mitemCart = itemSnapshot.getValue(itemCart.class);
updateitemCartList(mitemCart);
}
mProgressBar.setVisibility(View.INVISIBLE);
mshoppingListFragmentArrayAdapter = new ShoppingListFragmentArrayAdapter(getActivity(),R.layout.row_shopping_cart,itemCarts);
mListView.setAdapter(mshoppingListFragmentArrayAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return rootView;
}
private void updateitemCartList(itemCart item ) {
itemCarts.add(item);
}
private void clearItemCartList(){
itemCarts.clear();
}
}
When my checkbox is clicked or changed I need to obtain the id of the object itemCart this.
In this code in the itemCart object of my arrayAdapter always recive the last one that I have.
Aucun commentaire:
Enregistrer un commentaire