I have created an API: https://agenrumah.id/arumipro/Formdata and i need to get sumber[] from api and store it to my spinner. But i got error:
is there any best practice way to cast the jsonArray object to my spinner?
java.lang.ArrayStoreException: source[0] of type Item_sumber cannot be stored in destination array of type java.lang.String[]
Activity_addcontact.java
private ArrayList Arraylist_sumber, Arraylist_budget;
....
Call<Formdata> call = jsonPlaceHolderApi.getFormdata();
call.enqueue(new Callback<Formdata>() {
@Override
public void onResponse(Call<Formdata> call, Response<Formdata> response) {
Formdata jsonResponse = response.body();
String[] string_sumber = Arrays.copyOf(jsonResponse.getSumber(), jsonResponse.getSumber().length,String[].class);
ArrayAdapter<String> adapter_sumber = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, string_sumber);
dropdown_sumber.setAdapter(adapter_sumber);
dropdown_sumber.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
id_sumber = String.valueOf(i 1);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
@Override
public void onFailure(Call<Formdata> call, Throwable t) {
call.cancel();
}
});
}
Item_sumber
private String id;
private String nama_sumber;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNama_sumber() {
return nama_sumber;
}
public void setNama_sumber(String nama_sumber) {
this.nama_sumber = nama_sumber;
}
Formdata.java
public class Formdata {
private Item_sumber[] sumber;
private Item_budget[] budget;
public Item_sumber[] getSumber() {
return sumber;
}
public void setSumber(Item_sumber[] sumber) {
this.sumber = sumber;
}
public Item_budget[] getBudget() {
return budget;
}
public void setBudget(Item_budget[] budget) {
this.budget = budget;
}
}
CodePudding user response:
You can use ObjectMapper class of Jackson libraries. It will convert the response that are coming from API in object, syntax--
objectMapper om = new ObjectMapper();
om.readValue(response.body().asString(),FormData[].class);
I think, it would help you.
CodePudding user response:
you are storing Object elements from 'sumber' array in string array 'string_sumber'.
try this
JSONArray sumberArray = jsonresponse.getJsonArray("sumber");
String[] string_sumber = new String[sumberArray.length];
for(int i=0;i<sumberArray.length;i ){
string_summber[i] = get(i).toString();
}
CodePudding user response:
My friend helped me, and I've solved it.
Make and convert the jsonarray to Arraylist (object)
Make Iteration so you can store it to Array of String[]
Done! now cast it to your spinner
Call<Formdata> call = jsonPlaceHolderApi.getFormdata(); call.enqueue(new Callback<Formdata>() { @Override public void onResponse(Call<Formdata> call, Response<Formdata> response) { Formdata jsonResponse = response.body(); ArrayList<Item_sumber> arraylist_sumber = new ArrayList<>(Arrays.asList(jsonResponse.getSumber())); ArrayList<String> list_sumber = new ArrayList<>(); for (int i = 0; i < jsonResponse.getSumber().length; i ) { Item_sumber item_sumber = arraylist_sumber.get(i); list_sumber.add(item_sumber.getId()); } string_sumber = new String[arraylist_sumber.size()]; /* If 2 dimensional array convert private String[][] string_sumber2, string_budget2; string_sumber2 = new String[arraylist_sumber.size()][6]; for (int i = 0; i < jsonResponse.getSumber().length; i ) { Item_sumber data = arraylist_sumber.get(i); string_sumber2[i] = new String[]{data.getId(),data.getNama_sumber()}; }*/ for (int i = 0; i < jsonResponse.getSumber().length; i ) { Item_sumber data = arraylist_sumber.get(i); string_sumber[i] = data.getNama_sumber(); } ArrayAdapter<String> adapter_sumber = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, string_sumber); dropdown_sumber.setAdapter(adapter_sumber); dropdown_sumber.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { id_sumber = String.valueOf(i 1); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); @Override public void onFailure(Call<Formdata> call, Throwable t) { call.cancel(); } });