gpt4 book ai didi

java - onClickListeners 之间的变量重置为默认值

转载 作者:行者123 更新时间:2023-12-01 17:44:48 25 4
gpt4 key购买 nike

我不明白为什么我的变量被重置为默认值。我单击 listView,它将变量 currentAssessment 设置为检索到的记录的 ID。然后我单击删除具有所选 ID 的记录,它已被重置。

package com.breathetaichi.classtracker;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class CourseActivity extends AppCompatActivity {

private static DBInterface db;
private static ArrayList<Course> allCourses = new ArrayList<>();
private static ArrayList<Assessment> allAssessments = new ArrayList<>();
private static ArrayList<Instructor> allInstructors = new ArrayList<>();
private int courseCalled;
private int currentAssessment = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);

// currentAssessment = -1;

db = new DBInterface(CourseActivity.this);
db.getWritableDatabase();

allCourses = db.getCourses();
allAssessments = db.getAssessments();
allInstructors = db.getInstructors();

Bundle extras = getIntent().getExtras();
courseCalled = extras.getInt("courseID");

String courseTitle = "";
int courseTermID = -1;
int courseInstructorID = -1;
String courseStartDateStr = "";
String courseEndDateStr = "";
String courseStatusStr = "";
String courseNoteStr = "";
for(Course course : allCourses) {
if(course.getId() == courseCalled) {
courseTitle = course.getTitle();
courseTermID = course.getTermID();
courseInstructorID = course.getInstructorID();
courseStartDateStr = course.getStartDate();
courseEndDateStr = course.getEndDate();
courseStatusStr = course.getStatus();
courseNoteStr = course.getNote();
}
}

EditText courseName = findViewById(R.id.course_title);
courseName.setText(courseTitle);
EditText courseStartDate = findViewById(R.id.course_start_date);
courseStartDate.setText(courseStartDateStr);
EditText courseEndDate = findViewById(R.id.course_end_date);
courseEndDate.setText(courseEndDateStr);
EditText courseStatus = findViewById(R.id.course_status);
courseStatus.setText(courseStatusStr);
EditText termEndDate = findViewById(R.id.course_note);
termEndDate.setText(courseNoteStr);

int instructorID = courseInstructorID;
String instructorNameStr = "";
String instructorPhoneStr = "";
String instructorEmailStr = "";

for (Instructor instructor : allInstructors) {
if(instructor.getId() == instructorID) {
instructorNameStr = instructor.getName();
instructorPhoneStr = instructor.getPhone();
instructorEmailStr = instructor.getEmail();
}
}

EditText instructorName = findViewById(R.id.instructor_name);
instructorName.setText(instructorNameStr);
EditText instructorPhone = findViewById(R.id.instructor_phone);
instructorPhone.setText(instructorPhoneStr);
EditText instructorEmail = findViewById(R.id.instructor_email);
instructorEmail.setText(instructorEmailStr);

List<Assessment> assessmentsInCourse = new ArrayList<>();
for (Assessment assessment : allAssessments) {
if(assessment.getCourseID() == courseCalled) {
assessmentsInCourse.add(assessment);
}
}

String[] titles = new String[assessmentsInCourse.size()];
final int[] assessmentIDs = new int[assessmentsInCourse.size()];

int i = 0;
for(Assessment assessment : assessmentsInCourse) {
titles[i] = assessment.getTitle();
assessmentIDs[i] = assessment.getId();
i++;
}

final android.widget.ListAdapter adapter = new ListAdapter(this, titles);
final ListView lv = findViewById(R.id.assessment_list_view);

lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

int currentAssessment = assessmentIDs[(int)id];
System.out.println("set to clicked id, currentAssessment = " + currentAssessment);

Assessment assessment = getAssessment(currentAssessment);

String assessmentNameStr = assessment.getTitle();
String assessmentTypeStr = assessment.getType();
String assessmentDueStr = assessment.getDueDate();

EditText assessmentName = findViewById(R.id.assessment_title);
assessmentName.setText(assessmentNameStr);
EditText assessmentType = findViewById(R.id.assessment_type);
assessmentType.setText(assessmentTypeStr);
EditText assessmentDue = findViewById(R.id.assessment_due);
assessmentDue.setText(assessmentDueStr);

System.out.println("end of onclickListener, currentAssessment = " + currentAssessment);
}
});
System.out.println("After listener, currentAssessment = " + currentAssessment);
}

public void deleteCourseButton(View v) {

if(courseCalled == -1) {
Toast.makeText(CourseActivity.this,
"No saved term to delete,\nplease use back button.",
Toast.LENGTH_SHORT).show();

} else {
for(Course course : allCourses) {
if(course.getId() == courseCalled) {
db.deleteCourse(courseCalled);
TermActivity.numberOfCourses--;
}
}
this.recreate();
}
}

public void saveCourseButton(View v) {

Course saveCourse = new Course();

for(Course course : allCourses) {
if(courseCalled == course.getId()) {
saveCourse = course;
}
}

String courseTitle;
int courseTermID = saveCourse.getTermID();
int courseInstructorID = saveCourse.getInstructorID();
String courseStartDateStr;
String courseEndDateStr;
String courseStatusStr;
String courseNoteStr;

EditText courseName = findViewById(R.id.course_title);
courseTitle = courseName.getText().toString();
EditText courseStartDate = findViewById(R.id.course_start_date);
courseStartDateStr = courseStartDate.getText().toString();
EditText courseEndDate = findViewById(R.id.course_end_date);
courseEndDateStr = courseEndDate.getText().toString();
EditText courseStatus = findViewById(R.id.course_status);
courseStatusStr = courseStatus.getText().toString();
EditText courseNote = findViewById(R.id.course_note);
courseNoteStr = courseNote.getText().toString();

String instructorNameStr;
String instructorPhoneStr;
String instructorEmailStr;

EditText instructorName = findViewById(R.id.instructor_name);
instructorNameStr = instructorName.getText().toString();
EditText instructorPhone = findViewById(R.id.instructor_phone);
instructorPhoneStr = instructorPhone.getText().toString();
EditText instructorEmail = findViewById(R.id.instructor_email);
instructorEmailStr = instructorEmail.getText().toString();

if(courseTitle.isEmpty()
|| courseStartDateStr.isEmpty()
|| courseEndDateStr.isEmpty()
|| courseStatusStr.isEmpty()
|| courseNoteStr.isEmpty()) {

Toast.makeText(CourseActivity.this, "Please fill course fields.",
Toast.LENGTH_SHORT).show();

} else if(instructorNameStr.isEmpty()
|| instructorPhoneStr.isEmpty()
|| instructorEmailStr.isEmpty()) {

Toast.makeText(CourseActivity.this, "Please fill instructor fields.",
Toast.LENGTH_SHORT).show();
} else {
// Check if new course courseCalled is -1 or if not update with id in courseCalled
if(courseCalled == -1) {
courseTermID = TermActivity.activeTerm.getId();

// Insert new instructor and get their ID back
courseInstructorID = (int) db.insertInstructor(instructorNameStr,
instructorPhoneStr, instructorEmailStr);

courseCalled = (int) db.insertCourse(courseTermID,
courseInstructorID, courseTitle, courseStartDateStr,
courseEndDateStr, courseStatusStr, courseNoteStr);

allCourses = db.getCourses();

Toast.makeText(CourseActivity.this, "New course saved.",
Toast.LENGTH_SHORT).show();

this.recreate();
} else {
// Insert new instructor and get their ID back
courseInstructorID = (int) db.insertInstructor(instructorNameStr,
instructorPhoneStr, instructorEmailStr);

db.updateCourse(courseCalled, courseTermID, courseInstructorID, courseTitle,
courseStartDateStr, courseEndDateStr, courseStatusStr, courseNoteStr);

allCourses = db.getCourses();

Toast.makeText(CourseActivity.this, "Course updated.",
Toast.LENGTH_SHORT).show();

this.recreate();
}
}
}

public void saveAssessmentButton(View v) {

String assessmentNameStr;
String assessmentTypeStr;
String assessmentDueStr;

EditText assessmentName = findViewById(R.id.assessment_title);
assessmentNameStr = assessmentName.getText().toString();
EditText assessmentType = findViewById(R.id.assessment_type);
assessmentTypeStr = assessmentType.getText().toString();
EditText assessmentDue = findViewById(R.id.assessment_due);
assessmentDueStr = assessmentDue.getText().toString();

if(assessmentDueStr.isEmpty() || assessmentNameStr.isEmpty() || assessmentTypeStr.isEmpty()) {
Toast.makeText(CourseActivity.this, "Please fill all fields.",
Toast.LENGTH_SHORT).show();
} else {
if (currentAssessment == -1) {

currentAssessment = (int) db.insertAssessment(courseCalled, assessmentNameStr,
assessmentTypeStr, assessmentDueStr);

allAssessments = db.getAssessments();

Toast.makeText(CourseActivity.this, "Assessment added.",
Toast.LENGTH_SHORT).show();
this.recreate();
} else {

db.updateAssessment(currentAssessment, courseCalled, assessmentNameStr,
assessmentTypeStr, assessmentDueStr);

allAssessments = db.getAssessments();

Toast.makeText(CourseActivity.this, "Assessment updated.",
Toast.LENGTH_SHORT).show();
this.recreate();
}
}
}

public void newAssessmentButton(View v) {

EditText assessmentName = findViewById(R.id.assessment_title);
assessmentName.setText("");
EditText assessmentType = findViewById(R.id.assessment_type);
assessmentType.setText("");
EditText assessmentDue = findViewById(R.id.assessment_due);
assessmentDue.setText("");

currentAssessment = -1;
}

public void deleteAssessmentButton(View v) {
System.out.println("start of deleteAssessmentButton, currentAssessment = " + currentAssessment);
if(currentAssessment == -1) {
System.out.println("currentAssessment = " + currentAssessment);

EditText assessmentName = findViewById(R.id.assessment_title);
assessmentName.setText("");
EditText assessmentType = findViewById(R.id.assessment_type);
assessmentType.setText("");
EditText assessmentDue = findViewById(R.id.assessment_due);
assessmentDue.setText("");

Toast.makeText(CourseActivity.this, "No assessment chosen.",
Toast.LENGTH_SHORT).show();
} else {
System.out.println("sending to delete assessment id: " + currentAssessment);
long result = db.deleteAssessment(currentAssessment);

if(result == -1) {
Toast.makeText(CourseActivity.this, "Assessment NOT deleted.",
Toast.LENGTH_SHORT).show();

} else {
Toast.makeText(CourseActivity.this, "Assessment deleted. result = " + result,
Toast.LENGTH_SHORT).show();

allAssessments = db.getAssessments();

this.recreate();
}
}
}

public Assessment getAssessment(int id) {
Assessment assessment = new Assessment();

for(Assessment asmt : allAssessments) {
if(asmt.getId() == id) {
assessment = asmt;
}
}
return assessment;
}
}

我已经检查并重新检查了代码,并且在单击 listView 和单击删除按钮之间找不到应该重置 Activity 的位置。这是在我的 listView onClick 之后自动发生的吗?还有其他方法可以跟踪 ID 吗?

最佳答案

看起来您有两个 currentAssessment 变量。一个是类私有(private)的,另一个仅限于 onItemClick 的范围。一旦 onItemClick 退出,您就会丢失该分配,并且引用 currentAssessment 的其他任何地方都是属于该类的分配,它仍然是 -1。

修复方法是为 onItemClick 使用 currentAssessment 类。

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

currentAssessment = assessmentIDs[(int)id];
System.out.println("set to clicked id, currentAssessment = " + currentAssessment);

Assessment assessment = getAssessment(currentAssessment);

String assessmentNameStr = assessment.getTitle();
String assessmentTypeStr = assessment.getType();
String assessmentDueStr = assessment.getDueDate();

EditText assessmentName = findViewById(R.id.assessment_title);
assessmentName.setText(assessmentNameStr);
EditText assessmentType = findViewById(R.id.assessment_type);
assessmentType.setText(assessmentTypeStr);
EditText assessmentDue = findViewById(R.id.assessment_due);
assessmentDue.setText(assessmentDueStr);

System.out.println("end of onclickListener, currentAssessment = " + currentAssessment);
}
});

关于java - onClickListeners 之间的变量重置为默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56813767/

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