I have a JSON file with different Strings I want to create an altert dialog, but my String array is not working
I created a private String category. Into the category String I parse my String values with a for loop, in the method getJobArray. There the LogCat output tells me that the array gets filled with the String values. However, when I use the category String array in the onCreateDialog it tells me that the values are all null. I cannot even print the length to the LogCat
public class MultipleChoiceDialogFragment extends DialogFragment {
private RequestQueue mRQ;
private Context mContext;
private RequestQueue mRequestQ;
private static String TAG = "xd";
private String[] category;
public interface onMultiChoiceListener{
void onPositiveClicked(String[] list,ArrayList<String> selectedItemList);
void onNegativeButtonClicked();
}
onMultiChoiceListener mListener;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mRequestQ = Volley.newRequestQueue(context);
getJobArray();
try {
mListener = (onMultiChoiceListener) context;
}catch (Exception e){
throw new ClassCastException(getActivity() + "onMultiChoice not working error");
}
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final ArrayList<String> selectedItemList = new ArrayList<>();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//not working don't understand why... here category is null
final String[] list = category;
builder.setTitle("Select one")
.setMultiChoiceItems(list, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
if(b){
selectedItemList.add(list[i]);
}else{
selectedItemList.remove(list[i]);
}
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onPositiveClicked(list,selectedItemList);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onNegativeButtonClicked();
}
});
return builder.create();
}
private void getJobArray(){
//todo here I can get array from json array
String url = "http://api_staging.jab.poweredby.cnddts.at/mobile/metadata/categories?portal=1";
JsonObjectRequest request = new JsonObjectRequest(com.android.volley.Request.Method.GET, url, null,
new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("categories");
category = new String[jsonArray.length()];
Log.d(TAG, "onResponse: is getting array ready right now");
Log.d(TAG, "onResponse: jsonArray length" + jsonArray.length());
for(int i = 0; i < jsonArray.length(); i++){
JSONObject categories = jsonArray.getJSONObject(i);
category[i] = categories.getString("name");
Log.d(TAG, "onResponse: array input " + category[i]);
}
} catch (JSONException e) {
e.printStackTrace();
Log.d(TAG, "onResponse: catch caught...");
}
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQ.add(request);
}
}
Aucun commentaire:
Enregistrer un commentaire