【发布时间】:2018-06-25 13:11:54
【问题描述】:
我正在开发一个基本的测验应用程序,但卡在 API 部分。在 API 中,我们有“问题”、“选择”、“选择”和“正确”关键字。我想将“问题”和“选择”关键字的值保留在一个数组或不同的数组中,只要问题编号添加片段,还将这些问题和选择添加到片段活动中。我分享了它应该是什么样子的示例屏幕截图。 Layour没那么重要,我只需要Java代码。如果您能帮助我,我将不胜感激。谢谢。
这是我的 MainActivity.java
package myusarisoy.quizmasters;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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.JsonArrayRequest;
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 {
Frag frag = new Frag();
ViewPager vp;
RequestQueue requestQueue;
String[] abc, cba;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = findViewById(R.id.vp);
requestQueue = Volley.newRequestQueue(this);
FragmentPagerAdapter fpa = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(final int position) {
String url = "https://private-anon-a98702efdd-quizmasters.apiary-mock.com/questions";
final JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for(int i = 0; i < response.length(); i++) {
JSONObject jrResponse = response.getJSONObject(i);
String question = jrResponse.getString("question");
abc = new String[question.length()];
JSONArray choiceArray = jrResponse.getJSONArray("choices");
for(int j = 0; j < choiceArray.length(); j++) {
JSONObject currentChoice = choiceArray.getJSONObject(j);
String choice = currentChoice.getString("choice");
cba = new String[choice.length()];
String q = abc[i];
String c = cba[j];
Bundle b = new Bundle() ;
b.putString("tvQ", q);
b.putString("tvC", c);
frag.setArguments(b);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("myusarisoy", "Error...");
}
});
requestQueue.add(request);
return frag;
}
@Override
public int getCount() {
return abc.length;
}
};
// Set PagerAdapter to ViewPager
vp.setAdapter(fpa);
}
public void importantTask() {
Log.e("myusarisoy", "Important Task...");
}
}
这是我的 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
这是我的 Frag.java
package myusarisoy.quizmasters;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Frag extends Fragment {
View root;
TextView tvQuestion, tvChoices;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
root = inflater.inflate(R.layout.activity_frag, container, false);
tvQuestion = root.findViewById(R.id.tvq);
tvChoices = root.findViewById(R.id.tvc);
String tvQ = getArguments().getString("tvQ");
String tvC = getArguments().getString("tvC");
tvQuestion.setText(tvQ);
tvChoices.setText(tvC);
MainActivity ma = (MainActivity) getActivity();
ma.importantTask();
return root;
}
}
这是我的 activity_frag.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Frag">
<TextView
android:layout_marginTop="200sp"
android:id="@+id/tvq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:text="Question"
android:gravity="center"/>
<TextView
android:layout_marginTop="50sp"
android:id="@+id/tvc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="17sp"
android:textColor="#000000"
android:textStyle="bold"
android:text="Answers"
android:gravity="center"/>
</LinearLayout>
【问题讨论】:
-
您返回的是一个空片段,因为
onResponse几乎总是会在return frag;之后发生。换句话说,您的 Bundle 尚未制作,并且使用 Volley 到return该方法中的任何内容都行不通 -
无论如何,只需将 JSON 文本直接传递到 Fragment 即可。不要立即将其从数组中解析出来。那应该可以解决您要问的问题
-
感谢您的快速评论,我会看看。
-
其实我建议将整个 Volley 方法移到 Fragment 类中