gpt4 book ai didi

java - 错误: constructor in class cannot be applied to given types

转载 作者:行者123 更新时间:2023-12-02 11:09:32 27 4
gpt4 key购买 nike

如何将数据插入主键为(autoGenerate= true)的数据库?

这是表格的代码

@Entity(tableName = "Account")
public class Account {

@PrimaryKey(autoGenerate = true)
@ColumnInfo(index = true, name="ACCOUNT_ID")
public int id;

@NonNull
@ColumnInfo
private String bank;

@NonNull
@ColumnInfo
private String bal;

public Account(int id, String bank, String bal) {
this.id = id;
this.bank = bank;
this.bal = bal;}

public int getId(){return this.id;}
public String getBank(){return this.bank;}
public String getBal(){return this.bal;}
}

TableDao

@Dao
public interface AccountDao {
@Query("SELECT * from Account ORDER BY bank ASC")
LiveData<List<Account>> getAlphabetizedAccounts();


@Insert(onConflict = OnConflictStrategy.ABORT)
void insert(Account account);

@Query("DELETE FROM Account")
void deleteAll();
}

在startActivityForResult中我试图获取EditTextView数据,例如

    final Button save = findViewById(R.id.btn_save);
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(EtBankName.getText()) && TextUtils.isEmpty(EtBalView.getText())) {
setResult(RESULT_CANCELED, replyIntent);
} else {
String bankName = EtBankName.getText().toString();
String bal = EtBalView.getText().toString();
replyIntent.putExtra(_BANK_NAME, bankName);
replyIntent.putExtra(_AC_BAL, bal);
setResult(RESULT_OK, replyIntent);
}
Intent balIntent = new Intent();
if (TextUtils.isEmpty(EtBankName.getText()) && TextUtils.isEmpty(EtBalView.getText())) {
setResult(RESULT_CANCELED, replyIntent);
} else {
String bal = EtBalView.getText().toString();
replyIntent.putExtra(_AC_BAL, bal);
setResult(RESULT_OK, balIntent);
}

finish();
}
});

并且 onActivityresult 我正在这样做

  public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == NEW_ACCOUNT_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
Account account = new Account(data.getStringExtra(NewAccountActivity._BANK_NAME),data.getStringExtra(NewAccountActivity._AC_BAL));
mAccountViewModel.insert(account);
} else {
Toast.makeText(
getApplicationContext(),
R.string.empty_not_saved,
Toast.LENGTH_LONG).show();
}
}
}

Error Line:Account account = new Account(data.getStringExtra(NewAccountActivity._BANK_NAME),data.getStringExtra(NewAccountActivity._AC_BAL));

我已经尝试了所有删除添加但没有解决方案。我的构造函数有三个参数,我提供了两个。如何克服自动ID这个问题请确认任何更正。提前致谢。

最佳答案

根据来源:https://developer.android.com/reference/android/app/Activity#getIntent()Intent getIntent():返回启动activity的intent。因此,在 startActivityForResult 的情况下,使用 getIntent() 会更好,而不是使用 (new Intent())。

对于 @Primarykey(autoGenerate = True) 使用 0(零)。根据来源https://developer.android.com/reference/android/arch/persistence/room/PrimaryKey.html#autoGenerate()如果字段类型为 long 或 int (或其 TypeConverter 将其转换为 long 或 int),则插入方法在插入项时将 0(零)视为未设置。例如:

Table table =new Table(0, data1, data2,..);
mTable.insert(table);

终于我自己解决了我的问题。

关于java - 错误: constructor in class cannot be applied to given types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50696847/

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