[英]Using a class without creating a new instance of it
Newish to Java and very new to Android development. 对Java不熟悉,对Android开发又不陌生。
I have followed the following tutorial - Android tutorial (Basic Hello World App) and I am now changing it slightly as a proof of concept. 我遵循了以下教程-Android教程(Basic Hello World App) ,现在我将其稍作更改以作为概念证明。
Basically I want to use a class I have created but I am having some difficulties. 基本上,我想使用自己创建的类,但是遇到一些困难。 The class is shown below.
该类如下所示。
public class Employee {
private HashMap<String, String> employees = new HashMap<>();
public void setEmployees(String name, String jobTitle) {
employees.put(name, jobTitle);
System.out.println(employees);
}
public String getEmployees(String name){
return employees.get(name);
}
}
I populate the HashMap
from MainActivity.java
. 我从
MainActivity.java
填充HashMap
。 Using the set method above, this works as expected. 使用上面的set方法,这可以按预期工作。 I have tested it and I can see the HashMap has the required number of entries.
我已经对其进行了测试,并且可以看到HashMap具有所需的条目数。
My problem is when getting the data back. 我的问题是取回数据时。 How do I use the class.
我如何使用课程。 I have a file name
DisplayMessageActivity.java
and the following code within it. 我有一个文件名
DisplayMessageActivity.java
和其中的以下代码。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView employee_name = findViewById(R.id.employee_Name);
employee_name.setText(message);
TextView employee_title = findViewById(R.id.employee_Title);
employee_title.setText(employee.getEmployees(message));
}
}
The last line is where I am getting the error. 最后一行是我得到错误的地方。 This is because it doesnt know what
employee
is. 这是因为它不知道
employee
是什么。 I presume I need to add: 我想我需要添加:
Employee employee = new Employee;
If I add this within the onCreate
method it creates a new instance and therefore it has new values. 如果将其添加到
onCreate
方法中,它将创建一个新实例,因此具有新值。 I have also added it just above onCreate
with the same results. 我也将其添加到
onCreate
之上,具有相同的结果。
What am I missing? 我想念什么?
To retain the data you would want to make the variable and the methods static 要保留数据,您需要将变量和方法设为静态
public class Employee {
private static HashMap<String, String> employees = new HashMap<>();
public static void setEmployees(String name, String jobTitle) {
employees.put(name, jobTitle);
System.out.println(employees);
}
public static String getEmployees(String name){
return employees.get(name);
}
}
This means that only one version can exist at a time. 这意味着一次只能存在一个版本。 You would call the class directly and the method.
您将直接调用类和方法。
employee_title.setText(Employee.getEmployees(message))
Replace your Employee class code with below one 用下面的代码替换您的Employee类代码
public class Employee {
private static HashMap<String, String> employees;
public Employee() {
if (employees == null) {
employees = new HashMap<>();
}
}
public void setEmployees(String name, String jobTitle) {
employees.put(name, jobTitle);
System.out.println(employees);
}
public String getEmployees(String name) {
return employees.get(name);
}
}
Hope that helps you. 希望对您有帮助。
You can use Singleton in-memory cache
to keep your employees. 您可以使用
Singleton in-memory cache
来保留您的员工。
public class Employee {
private static sInstance;
private HashMap<String, String> employees = new HashMap<>();
private Employee(){
// No instance available
}
public static synchronized Employee getInstance(){
if(sInstance == null){
sInstance = new Employee();
}
return sInstance;
}
public void setEmployees(String name, String jobTitle) {
employees.put(name, jobTitle);
System.out.println(employees);
}
public String getEmployees(String name){
return employees.get(name);
}
}
Later you can use your in-memory cache like the following: 稍后,您可以使用内存缓存,如下所示:
Employee.getInstance().getEmployees(message);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.