java - 为什么从另一个方法android调用时我得到空的arrayList
问题描述
我正在尝试使用此方法 GetList() 填充 ArrayList,我可以看到我添加了 2 条记录。但是当我尝试使用此方法 LoadQst() 获取这些记录时,我在该 ArrayList 中获得了 0 条记录,这是我的代码,
public class GameActivity extends AppCompatActivity {
TextView texto1,texto2;
String Option1,Option2;
int CurrentQst,CurrentQstId,int1,int2;
private RequestQueue mQueue;
Question qst = new Question();
public final List<Question> Questions = new ArrayList<Question>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
texto1 = (TextView) findViewById(R.id.red);
texto2 = (TextView) findViewById(R.id.blue);
CurrentQst=0;
mQueue = Volley.newRequestQueue(GameActivity.this);
texto1.setText("");
GetList();
LoadQst();
texto1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GetList();
}
});
}
private void GetList() {
String url = "*********/api/get_all_products.php";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("products");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject employee = jsonArray.getJSONObject(i);
qst = new Question();
String option1 = employee.getString("option1");
qst.id=employee.getInt("id");
qst.option1=employee.getString("option1");
qst.option2=employee.getString("option2");
qst.vote1=employee.getInt("vote1");
qst.vote2=employee.getInt("vote2");
boolean added = Questions.add(qst);
if (added)
{
texto1.append("added\n");
} else
{
texto1.append("not added\n");
}
}
for (int i = 0; i < Questions.size(); i++) {
texto1.append("option " + Questions.get(i).option1 + "\n");
}
texto1.append(" size "+Questions.size()); //here i get size of 2
//Collections.shuffle(Questions);
} catch (JSONException e) {
e.printStackTrace();
texto1.setText(e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
private void LoadQst()
{
try {
Question CurrentQuestion ;
texto2.setText(String.valueOf(Questions.size()));//here i got an error size 0
// CurrentQuestion = (Question)Questions.get(CurrentQst);
//texto1.setText(CurrentQuestion.option1);
//texto2.setText(CurrentQuestion.option1);
// int1=CurrentQuestion.vote1;
//int2=CurrentQuestion.vote2;
//CurrentQstId=CurrentQuestion.id;
}catch (Exception e)
{
texto2.append(e.getMessage());
}
}
}
当我检查我的问题中是否有某些内容时,我得到了 2 个结果
for (int i = 0; i < Questions.size(); i++) {
texto1.append("option " + Questions.get(i).option1 + "\n");
}
但是当我在这个方法 LoadQst() 中调用这个 Questions ArrayList 时,我得到 0 个结果。我应该从 ArrayList 切换到另一种方法吗?你要建议任何类型的解决方案
请问,你能看一下吗?
解决方案
这是因为JsonObjectRequest
onResponse
异步中的方法。该LoadQst
方法在该方法之前执行onResponse
。
在里面调用你的LoadQst
方法onResponse
而不是调用 in onCreate
。
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("products");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject employee = jsonArray.getJSONObject(i);
qst = new Question();
String option1 = employee.getString("option1");
qst.id=employee.getInt("id");
qst.option1=employee.getString("option1");
qst.option2=employee.getString("option2");
qst.vote1=employee.getInt("vote1");
qst.vote2=employee.getInt("vote2");
boolean added = Questions.add(qst);
if (added)
{
texto1.append("added\n");
} else
{
texto1.append("not added\n");
}
}
for (int i = 0; i < Questions.size(); i++) {
texto1.append("option " + Questions.get(i).option1 + "\n");
}
texto1.append(" size "+Questions.size()); //here i get size of 2
//Collections.shuffle(Questions);
LoadQst(); // Call here.
} catch (JSONException e) {
e.printStackTrace();
texto1.setText(e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
推荐阅读
- laravel - Mapper 使用 Laravel 和 Elasticsearch 解析异常
- svg - 复制 svg 动画
- c# - 如何为声明创建自定义参数绑定?
- mysql - 有没有办法用特定的部分更新多个列?
- mysql - Magento 上的 MySQL 关闭问题
- laravel - 安装 Laravel DataTables Mongodb 插件
- android - 如何制作弹窗活动?
- javascript - 在 ng build --prod 期间无法解析验证指令中的所有参数
- ios - 从 Firebase 数据库中检索聊天消息列表时应用程序崩溃
- windows - Windows 资源管理器中的 FTP 列表命令输出视图格式不正确