gpt4 book ai didi

java - Android:值在插入房间数据库之前被转换为 0 或 1

转载 作者:行者123 更新时间:2023-12-01 18:10:34 25 4
gpt4 key购买 nike

我正在尝试将带有外键的实体存储到我的房间数据库中。假设我将外键值字段设置为 6。我一直跟踪它,直到插入它并且它保持为 6,但由于某种原因它被存储为 0。如果我没有实例在 6 的家长室数据库上,它会按预期崩溃。但即使它确实存在,它也会存储为 0。如果它是奇数,它会存储为 1。以下是一些相关代码:

@Entity(tableName = "order_table",
indices = {@Index(value = "shift_id")},
foreignKeys = @ForeignKey(entity = Shift.class,
parentColumns = "id",
childColumns = "shift_id",
onDelete = ForeignKey.CASCADE)})
public class Order {

@PrimaryKey(autoGenerate = true)
private int id;

@ColumnInfo(name = "shift_id")
private int shiftId;


public Order(int shiftId) {
this.shiftId = shiftId;
}

public int getId() {
return id;
}

public int getShiftId() {
return shiftId;
}

public void setId(int id) {
this.id = id;
}

@Dao
public interface OrderDao {

@Insert
void insert(Order order);
}

@Query("SELECT * FROM order_table WHERE shift_id = :shiftId")
LiveData<List<Order>> getAllOrdersFromShift(int shiftId);
}
public class OrderRepository {

private OrderDao orderDao;

public OrderRepository(Application application) {
ShiftsDatabase database = ShiftsDatabase.getInstance(application);
orderDao = database.orderDao();

}

public void insert(Order order) {
new InsertOrderAsyncTask(orderDao).execute(order);

}
public LiveData<List<Order>> getAllOrdersFromShift(List<Integer> ids) {
return orderDao.getAllOrdersFromShift(ids.get(0));
}

private static class InsertOrderAsyncTask extends AsyncTask<Order, Void, Void> {
private OrderDao orderDao;

public InsertOrderAsyncTask(OrderDao orderDao) {
this.orderDao = orderDao;
}

@Override
protected Void doInBackground(Order... orders) {
orderDao.insert(orders[0]);
return null;
}
}
}
public class OrderViewModel extends AndroidViewModel {

private OrderRepository repository;

public OrderViewModel(Application application) {
super(application);
repository = new OrderRepository(application);
}

public void insert(Order order) {
repository.insert(order);
}
public LiveData<List<Order>> getAllOrdersFromShift(int shiftID){
List<Integer> ids = new ArrayList<>();
ids.add(shiftID);
return repository.getAllOrdersFromShift(ids);
}
}

注意:我知道数组列表似乎没有必要,但我只是删除了简化所需的代码。

public class OrderFragment extends Fragment {

private OrderViewModel orderViewModel;
private RecyclerView recyclerView;
private int shiftId;
private Button saveButton;

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.order_fragment, container, false);

recyclerView = view.findViewById(R.id.tipsRecyclerView);
recyclerView.addItemDecoration(new DividerItemDecoration(this.getActivity(), LinearLayout.VERTICAL));
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

final OrderAdapter adapter = new OrderAdapter();
recyclerView.setAdapter(adapter);

orderViewModel = ViewModelProviders.of(this).get(OrderViewModel.class);

orderViewModel.getAllOrdersFromShift(shiftId).observe(getViewLifecycleOwner(), new Observer<List<Order>>() {
@Override
public void onChanged(List<Order> orders) {
adapter.submitList(orders);
}
});
saveButton = view.findViewById(R.id.saveButton);

saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Order newOrder = new Order(shiftId);
orderViewModel.insert(newOrder);
}
}

如果将getAllOrdersFromShift中的shiftId改为0,则会显示一些记录。一个奇怪的症状是,如果我尝试将订单存储到 shiftId 为 0 的类次,则会收到外键错误。

我确实在这里找到了有类似问题的人:Room database: inserted ID is always 0

他们只是使用 Kotlin,而我还不知道。

最佳答案

这最终成为查询的问题,在这里无法检测到它,因为我认为这并不重要。原来是这样

关于java - Android:值在插入房间数据库之前被转换为 0 或 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60475990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com