如何在加载json数据或对象时显示加载屏幕,然后开始新活动?



我想在加载json数据或对象时显示加载屏幕,并用我加载的所有json对象开始一个新的活动。

我的代码模式类

public catagory_data_modal(String catagorytitle, String poster) {
this.catagorytitle = catagorytitle;
this.poster = poster;
}
public void setCatagorytitle(String catagorytitle) {
this.catagorytitle = catagorytitle;
}
public void setPoster(String poster) {
this.poster = poster;
}
public String getCatagorytitle() {
return catagorytitle;
}
public String getPoster() {
return poster;
}
}

我的主要活动类

package com.sp2550.mstream;
public class MainActivity extends AppCompatActivity {
ArrayList<catagory_data_modal> mmodaldata;
RequestQueue mRequestque;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mmodaldata = new ArrayList<>();
mRequestque = Volley.newRequestQueue(this);
parsejson();
}
private void parsejson() {
String url = "https://api.airtable.com/v0/apppr49hyF0uoy8hV/Table%201?api_key=key3fE0";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray array = response.getJSONArray("records");
for (int i = 0; i < array.length();i++){
JSONObject object = array.getJSONObject(i);
JSONObject jsonObject  = object.getJSONObject("fields");
String poster = jsonObject.getString("background");
String companyname = jsonObject.getString("moviecompany");
mmodaldata.add(new catagory_data_modal(companyname,poster));
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
Toast toast=Toast.makeText(getApplicationContext(),"data ",Toast.LENGTH_SHORT);
toast.show();
Intent intent = new Intent(MainActivity.this,Dashboard.class).putExtra("data",mmodaldata);
MainActivity.this.startActivity(intent);
MainActivity.this.finish();
mRequestque.add(request);
}
}

请在响应函数内部调用启动活动函数。

ArrayList<catagory_data_modal> mmodaldata;
RequestQueue mRequestque;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mmodaldata = new ArrayList<>();
mRequestque = Volley.newRequestQueue(this);
parsejson();
}
private void parsejson() {
String url = "https://api.airtable.com/v0/apppr49hyF0uoy8hV/Table%201?api_key=key3fE0";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Intent intent = new Intent(MainActivity.this,Dashboard.class).putExtra("data",mmodaldata);
MainActivity.this.startActivity(intent);
MainActivity.this.finish();
JSONArray array = response.getJSONArray("records");
for (int i = 0; i < array.length();i++){
JSONObject object = array.getJSONObject(i);
JSONObject jsonObject  = object.getJSONObject("fields");
String poster = jsonObject.getString("background");
String companyname = jsonObject.getString("moviecompany");
mmodaldata.add(new catagory_data_modal(companyname,poster));
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
Toast toast=Toast.makeText(getApplicationContext(),"data ",Toast.LENGTH_SHORT);
toast.show();
mRequestque.add(request);
}

最新更新