【发布时间】:2018-07-07 03:57:43
【问题描述】:
我认为我面临的问题是因为 ui 在从后端获取数据之前绘制了我的布局拳头,这是微调器...所以我需要先阻止它并等待我的数据,但我不知道如何 我从后端获取的json数据是这样的,
[
{
"id": 3,
"nom": "xxxx",
"etat": 0
}
]
我正在尝试使用从后端获得的 JSON 数据来填充微调器的数据。即使我没有收到任何错误,但仍然没有得到我想要的结果。
我的课是这样的,
// URL
String URL= AppConfig.URL_GetFolders;
Spinner spinner;
ArrayList<String> Folders;
NavigationView navigationView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.folder_activity);
init(savedInstanceState);
// declaring spinner refs
Folders=new ArrayList<>();
spinner=(Spinner)findViewById(R.id.folder_Name);
// Spinner section
loadSpinnerFolders(URL);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String folder = spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();
Toast.makeText(getApplicationContext(),folder,Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
}
而我的spinner函数是这样的,
private void loadSpinnerFolders (String url) {
RequestQueue requestQueue=Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray=jsonObject.getJSONArray("data");
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String dossier = jsonObject1.getString("nom");
Folders.add(dossier);
}
spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, Folders));
}catch (JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
int socketTimeout = 30000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
requestQueue.add(stringRequest);
}
而我的文件夹布局是这样的,
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_activity_DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- The main content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<FrameLayout
android:id="@+id/main_activity_content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="@+id/folder_Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:paddingBottom="20dp"
android:paddingEnd="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingStart="20dp"
android:paddingTop="20dp" />
</FrameLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
【问题讨论】:
-
你能显示完整的 json 吗?因为您的代码执行此 jsonObject.getJSONArray("data");.... 但没有数据数组
-
对不起,我只是把它编辑了,我得到的 json
-
调试它。应该工作。文件夹的大小是多少?在 for 循环之后检查它
-
我面临的问题是用户界面在从后端获取数据之前绘制了我的布局拳头,这是微调器...所以我需要先阻止它并等待我的数据,但我没有不知道怎么办
-
然后添加一个处理程序。生病添加代码的答案。