I am newbie to android development. I am working in android studio
. I have some json
files that i have placed inside my assets
folder. Now i want to populate the data in json file(s) into a spinner. I do know how to use spinner and populate data using web service
but i don't have a clue how to do it with json
file(s). Moreover i couldn't find any tutorial on that but yes i did find how to read data from json file, but i want to populate it in spinner. Below is one of my json
file data .
ref.json
{"reference":
[
{"ref_no":"11111111111111","Name":"Faisal"},
{"ref_no":"22222222222222","Name":"Salman"},
{"ref_no":"33333333333333","Name":"Asim"},
{"ref_no":"44444444444444","Name":"Asad"},
{"ref_no":"55555555555555","Name":"Mateen"},
{"ref_no":"66666666666666","Name":"Omar"},
{"ref_no":"77777777777777","Name":"Usama"}
]}
Below is my layout for spinner
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dd_ref"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
I want to get ref_no
from JSON file and show them into the spinner.
Update 1
After reading the answers and suggestions i have done the following
ArrayList<String> refList;
public static final String LOG_TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
loadData();
} catch (JSONException e) {
e.printStackTrace();
}
}
public void loadData() throws JSONException {
JSONArray jsonArray;
JSONObject jobj;
try
{
jobj = new JSONObject(loadJSONFromAssest());
jsonArray = jobj.getJSONArray("reference");
for(int i = 0; i<jsonArray.length();i++)
{
jobj = jsonArray.getJSONObject(i);
Log.d("Details-->", jobj.getString("ref_no"));
String ref = jobj.getString("ref_no");
refList.add(ref);
}
} catch (JSONException e)
{
e.printStackTrace();
}
dd_ref.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, refList));
dd_ref.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
refr_no = String.valueOf(refList.get(position));
Log.i(LOG_TAG,refr_no);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public String loadJSONFromAssest() {
String json = " ";
try{
InputStream is = getAssets().open("ref.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
return null;
}
return json;
}
When i run the application i get the following error
FATAL EXCEPTION: main
Process: com.example.accurat.application, PID: 7336
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.accurat.application/com.example.accurat.application.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2506)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5491)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.example.accurat.application.MainActivity.loadData(MainActivity.java:59)
at com.example.accurat.application.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6270)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2506)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5491)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Below is the image for my debugging
The error is at refList.add(ref);
as it tells that the refList
is null.
I am stuck to it and didn't know how to do it.
Any help would be highly appreciated.