I'm working on some code to insert a employee into a database when I do this for me I've done Database.dao().addEmployee(emp); it says non static method 'dao' cannot be referenced from a static context. If I add it to static then the addEmployee() becomes an error as well. I am unsure what this means as I am a very beginner. My dao is an interface for the database if that helps make sense of what I am trying to do. Any help would be muchly appreciated. Here is my code:
package com.example.a2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public static Database database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database = Room.databaseBuilder(getApplicationContext(), Database.class, "employeedb").allowMainThreadQueries().build();
final Button logInBt = findViewById(R.id.buttonLogIn);
final EditText userEditText = findViewById(R.id.editTextTextPersonName);
final EditText passwordEditText = findViewById(R.id.editTextTextPassword);
logInBt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Inventory.class);
startActivity(i);
// Obtaining data from the interface
String userName = userEditText.getText().toString();
String password = passwordEditText.getText().toString();
// Create new employee object
Employee emp = new Employee();
emp.setUserName(userName);
emp.setPassword(password);
//Insert customer into database
Database.dao().addEmployee(emp);
//Display a message
Toast.makeText(getBaseContext(), "Added successfully!", Toast.LENGTH_SHORT).show();
// Clear the text
userEditText.setText("");
passwordEditText.setText("");
}
});
}
}