错误:类中的构造函数不能应用于给定类型

时间:2018-06-05 09:36:22

标签: java android android-room

如何将数据插入主键为(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));

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

1 个答案:

答案 0 :(得分:1)

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

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

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

最后我自己解决了我的问题。