我正在使用firebase在android中创建购物列表应用程序,我获取了项目列表,并在数组适配器中显示良好。当我需要检查项目并在Firebase中更改它时。我不知道是否必须在数组适配器或片段中执行此操作。我的课程是itemCart,并从另一个有ID的食物延伸。 我的问题是当我检查项目时,我无法访问包含此复选框的对象,我该怎么办?
我有这堂课:
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;
}
}
我也有这个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;
}
}
在适配器中执行此操作是正确的还是我必须在我的片段中执行此操作?
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();
}
}
单击或更改我的复选框时,我需要获取对象itemCart的ID。
在我的arrayAdapter的itemCart对象中的这段代码中,总是重复我拥有的最后一个。
答案 0 :(得分:0)
首先,您必须创建界面
interface ItemClickListener{
void onItemClicked(ItemCart mItemCart);
}
而不是更新您的适配器
private ItemClickListener itemClickListener
public ShoppingListFragmentArrayAdapter(@NonNull Context context, int resource, List<itemCart> objects,
ItemClickListener itemClickListener) {
super(context, resource, objects);
this.listItemsCart = objects;
this.itemClickListener = itemClickListener;
}
checkBoxFood.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemClickListener.onItemClicked(mItemCart);
}
});
并像这样将侦听器传递给适配器
new ShoppingListFragmentArrayAdapter(getActivity(), R.layout.row_shopping_cart, itemCarts, new ItemClickListener() {
@Override
public void onItemClicked(ItemCart itemCart) {
// You code to handle clicked item
}
});
<强>更新强> 像这样制作你的阵列适配器
public class ShoppingListFragmentArrayAdapter extends ArrayAdapter<itemCart> {
List<itemCart> listItemsCart;
FirebaseDatabase mFirebaseDatabase;
String uid;
FirebaseAuth mFirebaseAuth;
DatabaseReference mitemCardReference;
private ItemClickListener itemClickListener;
public ShoppingListFragmentArrayAdapter(@NonNull Context context, int resource, List<itemCart> objects,
ItemClickListener itemClickListener) {
super(context, resource, objects);
this.listItemsCart = objects;
this.itemClickListener = itemClickListener;
}
@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);
itemCart 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) {
itemClickListener.onItemClicked(mItemCart);
}
});
return convertView;
}
}