为什么我不能在模型中使用"this"(MVC 模式)?



我是Android Studio和Java的新手。我想知道为什么我不能使用";这个";在模型类中。我得到以下错误消息:";不兼容的类型:模型无法转换为上下文;。如果我在Notes类中编写代码,代码就可以正常工作。错误出现在Model中(在我创建按钮的地方,"this"在Android Studio中用红色下划线(。

package com.example.notesapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class Notes extends AppCompatActivity implements View.OnClickListener {
private Controller controller;
private EditText etNotesCreationHeader;
private EditText etNotesCreationText;
private Button btnNotesCreate;
private Button btnNotesCreationCreate;
private TextView tvNotesHeader;
private TextView tvNotesCounter;
public int counter;
// נגדיר משתנה מסוג SharedPreferences
private SharedPreferences sharedPref;
// כדי לכתוב ל Shared Preferences נגדיר Editor שלו
private SharedPreferences.Editor editor;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes);
sharedPref = this.getSharedPreferences(String.valueOf(R.string.shared_preferences_file_name), MODE_PRIVATE);
editor = sharedPref.edit();
initViews();
Intent intent = getIntent();
String name = intent.getStringExtra("name");
tvNotesHeader.setText("Welcome back, " + name + "!");
tvNotesCounter.setText("Number of Notes: " + sharedPref.getInt("counter", 0));
counter = sharedPref.getInt("counter", -1);
}
private void initViews() {
tvNotesHeader = findViewById(R.id.tvNotesHeader);
tvNotesCounter = findViewById(R.id.tvNotesCounter);
btnNotesCreate = findViewById(R.id.btnNotesCreate);
etNotesCreationHeader = findViewById(R.id.etNotesCreationHeader);
etNotesCreationText = findViewById(R.id.etNotesCreationText);
btnNotesCreationCreate = findViewById(R.id.btnNotesCreationCreate);
btnNotesCreate.setOnClickListener(this);
btnNotesCreationCreate.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view == btnNotesCreate) {
etNotesCreationHeader.setVisibility(view.VISIBLE);
etNotesCreationText.setVisibility(view.VISIBLE);
btnNotesCreationCreate.setVisibility(view.VISIBLE);
Toast.makeText(this, "Click 'Create' to finish!", Toast.LENGTH_SHORT).show();
}
if (view == btnNotesCreationCreate) {
if ((etNotesCreationText.getText().toString().trim().equals("")) || etNotesCreationHeader.getText().toString().trim().equals("")) {
Toast.makeText(this, "Please fill the fields!", Toast.LENGTH_SHORT).show();
} else {
//קטע יצירת הפתקים!
etNotesCreationHeader.setVisibility(view.INVISIBLE);
etNotesCreationText.setVisibility(view.INVISIBLE);
btnNotesCreationCreate.setVisibility(view.INVISIBLE);
//Toast.makeText(this, "The Note was created!", Toast.LENGTH_SHORT).show();
etNotesCreationHeader.setText("");
etNotesCreationText.setText("");
if (-1 == sharedPref.getInt("counter", -1)) {
counter = 0;
editor.putInt("counter", counter);
editor.commit();
} else {
counter += 1;
editor.putInt("counter", counter);
editor.commit();
tvNotesCounter.setText("Number of Notes: " + String.valueOf(sharedPref.getInt("counter", 0)));
this.controller.createNote();
}
}
//כאן צריך להוסיף counter כדי שיוסיך ל-id של כל note מספר שונה, ובכך נשיג id ייחודי לכל note. בנוסף צריך להוסיף את ה-counter ל-shared preferences כדי שהוא תמיד יזכר על המכשיר ותמיד רק יגדל. ניתן להתשמש ב-id גם כשם במסמך של ה-shared preferences!
}
}
}

控制器:

package com.example.notesapplication;
public class Controller
{
private Model model;
public Controller() {
this.model = new Model();
}
public void createNote() { this.model.createNote(); }
}

型号:

package com.example.notesapplication;
import android.app.Activity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Model {
public void createNote() {
Button myButton = new Button(this);
}
}
Button构造函数的第一个参数必须是Context的实例。https://developer.android.com/reference/android/widget/Button#Button(android.content.Context(

您的NotesContext,因为它扩展了AppCompatActivity,并且(最终(扩展了Context

您的Model不是Context,因为它没有扩展任何此类类型。

更重要的是,在MVC模式中,按钮本质上是而不是模型的一部分。模型是信息的表示,而不是UI元素。因此,无论是什么想法让你认为你需要在你的模型中有一个按钮,你都应该重新思考