[英]How to add an element to an array list from one activity and from another activity
I am making an app that consists of multiple choice questions. 我正在制作一个包含多项选择题的应用。 Each question has its own activity, for example QuestionTwoActivity
. 每个问题都有自己的活动,例如QuestionTwoActivity
。 Every time the user clicks on a button, for example a button for answer A, it moves them to the next activity, for example from QuestionTwoActivity
to QuestionThreeActivity
. 每次用户单击按钮(例如答案A的按钮)时,都会将其移至下一个活动,例如,从QuestionTwoActivity
到QuestionThreeActivity
。 If the answer is correct, it increases their mark by 3. It has ten questions and a total possible mark of 30. 如果答案是正确的,则将其分数加3。它有10个问题,可能的总分是30。
That part of my app worked well, but the problem started when I wanted to make a final preview that appears on the GradeActivity
which initially only showed the final grade of the user. 我的应用程序的那部分运行良好,但是当我想制作显示在GradeActivity
上的最终预览(最初仅显示用户的最终成绩)时,问题就开始了。 I wanted to add a list to the grade activity that showed the answer of the answer of the user for each question, and if the user answered correctly or not, so I made a custom arrayAdapter to populate the list, and then tried to use the onclick method of each answer to add an element to the arraylist of the arrayadapter based on the question answered, but it caused the app to crash 我想向成绩活动中添加一个列表,以显示用户对每个问题的答案,如果用户回答正确与否,那么我制作了一个自定义arrayAdapter来填充列表,然后尝试使用每个答案的onclick方法根据所回答的问题将元素添加到arrayadapter的arraylist中,但这导致应用程序崩溃
Here is the code for the gradeActivity
which opens only after the tenth question is answered: 这是gradeActivity
的代码,仅在回答第十个问题后才打开:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class Grade extends AppCompatActivity{
examark mExamGrade; // declaring an instance of the examark class to call getMark method
double mark; // an int object to store the exam mark in it
TextView mGradeTextView;
TextView mNameTextView;
ListView mPreviewListView;
static ArrayList<answers_preview_data> mPreviewArrayList;
PreviewAdapter mPreviewAdapter;
examark name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grade);
int colorRed = getResources().getColor(R.color.colorRed);
int colorOrange = getResources().getColor(R.color.colorOrange);
int colorYellow = getResources().getColor(R.color.colorYellow);
int colorGreen = getResources().getColor(R.color.colorGreen);
mExamGrade = new examark();
name = new examark();
mark = mExamGrade.getExamMark();// storing the exam mark in the mark variable
mGradeTextView = (TextView) findViewById(R.id.grade); // initializing the view responsible for showing the final mark
mGradeTextView.setText(String.valueOf(mark));
mNameTextView = (TextView) findViewById(R.id.name_text_view);
String nameOfQuizzer = name.getNameOfQuizTaker();
mNameTextView.setText(nameOfQuizzer);
mPreviewArrayList = new ArrayList<answers_preview_data>();
mPreviewAdapter = new PreviewAdapter(this, mPreviewArrayList);
mPreviewListView = (ListView) findViewById(R.id.preview_list);
mPreviewListView.setAdapter(mPreviewAdapter);
// a conditional statement to change the color of the grade based on how good the preformence
if ( mark > 0.0 && mark < 10.0 ) {
mGradeTextView.setTextColor(colorRed);
} else if (mark > 10.0 && mark <= 15.0) {
mGradeTextView.setTextColor(colorOrange);
} else if (mark > 15.0 && mark < 20.0) {
mGradeTextView.setTextColor(colorYellow);
} else if (mark > 20.0) {
mGradeTextView.setTextColor(colorGreen);
}
}
}
Here is the code for one of the answers onclickListner: 以下是答案onclickListner之一的代码:
mAnswerATextView.setText("A-Labour Party");
mAnswerATextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Grade.mPreviewArrayList.add(new answers_preview_data("1", "the answer of question A" , " the true answer of question 1", R.color.colorGreen, R.drawable.correct));
Intent openQuestion = new Intent(QuestionOne.this ,QuestionTwo.class);
startActivity(openQuestion);
mExamark.incrementMark();
}
});
Here is what the debugger menu says when the answer button is clicked: 这是单击“答案”按钮时调试器菜单显示的内容:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.xams, PID: 16254
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.example.admin.xams.QuestionOne$1.onClick(QuestionOne.java:52)
at android.view.View.performClick(View.java:4848)
at android.view.View$PerformClick.run(View.java:20270)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5667)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:962)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Is there a way to add an element to an arraylist from other activities before the activity that contains the arraylist is opened (set to onCreate)? 有没有办法在打开包含arraylist的活动(设置为onCreate)之前将元素从其他活动添加到arraylist?
You can implement one of the following options: 您可以实现以下选项之一:
Activity
's constructor instead of onCreate()
method 不好但是很快-在Activity
的构造函数中初始化列表,而不是onCreate()
方法 You could follow the Singleton pattern. 您可以遵循Singleton模式。 In essence, you want to make one global list that you can access from anywhere. 本质上,您想创建一个全局列表,可以从任何地方访问。 You could put this in a few places, I would suggest extending Application
and putting it there for a good start. 您可以将它放在几个地方,我建议您扩展Application
并将其放在一个好的开始。
You may have your own Application
, but if not, you'll also need to register it in your manifest. 您可能拥有自己的Application
,但如果没有,则还需要在清单中进行注册。
So, I created an app called MyApp
, which extends from Application
. 因此,我创建了一个名为MyApp
的应用程序,该Application
从Application
扩展而来的。 This will contain the list of answers that we can access from anywhere. 这将包含我们可以从任何地方访问的答案列表。
public class MyApp extends Application {
// A public static list of answers
public static ArrayList<answers_preview_data> answers = new ArrayList<>();
@Override
public void onCreate() {
super.onCreate();
// do other app setup ...
}
}
Now, within your questions, we can easily add a new answer to this list. 现在,在您提出的问题中,我们可以轻松地向此列表添加新答案。
public class QuestionOne extends AppCompatActivity implements View.OnClickListener {
@Override
public void onClick(View v) {
// Adding the new answer to your list.
MyApp.answers.add(new answers_preview_data());
}
}
And within your Grade class, we can fetch the list and use it locally. 在您的Grade类中,我们可以获取列表并在本地使用。
public class Grade extends AppCompatActivity {
private ArrayList<answers_preview_data> mPreviewArrayList;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPreviewArrayList = MyApp.answers;
// use your list ...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.