我无法传递jsonParse方法变量。我试图将它们保存在“ ArrayList”中,但该数组保持为空。用另一种方法,例如。 “测试”,它的工作原理。 为什么?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = findViewById(R.id.griglia);
mQueue = Volley.newRequestQueue(this);
txt = findViewById(R.id.titolo);
movieList= new ArrayList<Movie>();
// 我将方法jsonParse称为用于填充我的ArrayList
jsonParse();
if(movieList.isEmpty()){
txt.setText("vuoto");
}
else{
txt.setText("pieno");
}
}
//我对此代码有疑问。他工作了,但是没有填写ArrayList
public void jsonParse(){
String url ="https://api.myjson.com/bins/tn67c";
JsonObjectRequest request= new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray= response.getJSONArray("movie");
for(int i=0; i<jsonArray.length(); i++){
JSONObject emplyee = jsonArray.getJSONObject(i);
String titolo = emplyee.getString("titolo");
String url = emplyee.getString("url");
String icona= emplyee.getString("icona");
Movie movie= new Movie();
movie.setTitolo(titolo);
movie.setVideoFilePath(url);
movieList.add(i, movie);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
// 我相信示例方法“ prova”现在就可以使用。为什么?
public void prova(){
int i=0;
for(i=0; i<2; i++){
String test= "ciao " + i;
Movie movie= new Movie();
movie.setTitolo(test);
movieList.add(movie);
}
}
答案 0 :(得分:0)
您的jsonParse
似乎使用了回调onResponse(JSONObject response)
,因此它是在后台线程上执行的。
尝试调试它,在onResponse
方法中设置断点,然后在此处将列表设置为TextView。
我认为它将首先在您将文本设置为TextView之后再以onResponse
方法停止
答案 1 :(得分:0)
您是在致电请求后初始化movieList
,请先致电,然后在onCreate
之前尝试初始化您的列表
ArrayList<Movie> movieList = new ArrayList<Movie>();
并放入
if(movieList.isEmpty()){
txt.setText("vuoto");
} else{
txt.setText("pieno");
}
在列表中添加完项目之后立即显示
编辑:
package com.example.zvideo2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
String url ="https://api.myjson.com/bins/tn67c";
private RequestQueue mQueue;
ArrayList<Movie> movieList= new ArrayList<Movie>();
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mQueue = Volley.newRequestQueue(this);
txt = findViewById(R.id.titolo);
jsonParse();
// prova();
}
public void jsonParse(){
String url ="https://api.myjson.com/bins/tn67c";
JsonObjectRequest request= new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray= response.getJSONArray("movie");
for(int i=0; i<jsonArray.length(); i++){
JSONObject emplyee = jsonArray.getJSONObject(i);
String titolo = emplyee.getString("titolo");
String url = emplyee.getString("url");
String icona= emplyee.getString("icona");
Movie movie= new Movie();
movie.setTitolo(titolo);
movie.setVideoFilePath(url);
movieList.add(i, movie);
}
Toast.makeText(getActivity(), "item add successfully", Toast.LENGTH_LONG).show();
if(movieList.isEmpty()){
txt.setText("vuoto");
}else{
txt.setText("pieno");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
mQueue.start(request);
}
public void prova(){
int i=0;
for(i=0; i<2; i++){
String test= "ciao " + i;
Movie movie= new Movie();
movie.setTitolo(test);
movieList.add(movie);
}
}
}