如何向我的Firebase数据库添加复选框值? 现在,我正在使用edittext将项目存储在数据库中。我希望它可以通过复选框进行检查。而且即使我重新启动应用程序,它也可以保存复选框状态。
我在这里搜索了答案,但是没有运气:( 所以我想问问自己。
模型类:
public class Items {
String itemId;
String itemname;
public items(){
}
public Items (String itemId, String itemname){
this.itemId = itemId;
this.itemname = itemname;
}
public String getItemId() {
return itemId;
}
public String getItemname() {
return itemname;
}
适配器:
public class listItems extends ArrayAdapter<Items> {
private Activity context;
private List<Items> listitems;
public listItems(Activity context, List<Items> listitems){
super (context, R.layout.list_items, listitems);
this.context = context;
this.listitems = listitems;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_items, null, true);
TextView tvitemname = (TextView) listViewItem.findViewById(R.id.tvitemname);
CheckBox checkBox = (CheckBox) listViewItem.findViewById(R.id.checkbx);
Items items = listitems.get(position);
tvitemname.setText(items.getItemname());
return listViewItem;
}
添加项目活动
private void additem(){
String itemname = etitemname.getText().toString().trim();
if (!TextUtils.isEmpty(itemname)) {
String id = databaseItems.push().getKey();
Item item = new Item (id, itemname);
databaseItems.child(id).setValue(item);
Toast.makeText(this, "Item added", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Itemname missing...", Toast.LENGTH_LONG).show();
}
}
}