user7197369
user7197369

Reputation:

Android coding Populate Json Value to Spinner get all values of selected to TextView or Edit

Hi Greetings and Salutations. Am trying to develop an android app that during the signup data is fetched from Json stored at the asset folder and populate it in spinner. In the code i have one spinner and multiple EditView and TextView. In the signup the user select country from the spinner and it should automatically set some values to the TextView or EditView like country code, country phone code, id etc but am finding it difficult to do so, have searched but most people only have two value like id and name while mine has more. The code am modifying i got it somewhere from this site few weeks ago.

i added this to the code

     ArrayList<String> countryName = new ArrayList<String>();
     ArrayList<String> phoneCode = new ArrayList<String>();
     ArrayList<String> countryCode = new ArrayList<String>();

and this

   country = jObj.getString("name");
   dial_code = jObj.getString("dial_code");
   country_code = jObj.getString("code");
   countryName.add(country);
   phoneCode.add(dial_code);
   countryCode.add(country_code);

    TextCountry = (TextView) findViewById(R.id.country);
    TextDialCode = (TextView) findViewById(R.id.dial_code);
    TextCode = (TextView) findViewById(R.id.code);


    json_string= loadJSONFromAsset();

    ArrayList<String> countryName = new ArrayList<String>();
    ArrayList<String> phoneCode = new ArrayList<String>();
    ArrayList<String> countryCode = new ArrayList<String>();

    {

        try {
            jsonObj =new JSONObject(json_string);
            jsonArray =jsonObj.getJSONArray("countries");
            String country, dial_code, country_code;
            for (int i = 0; i < jsonArray.length(); i++){
                JSONObject jObj = jsonArray.getJSONObject(i);
                country = jObj.getString("name");
                dial_code = jObj.getString("dial_code");
                country_code = jObj.getString("code");
                countryName.add(country);
                phoneCode.add(dial_code);
                countryCode.add(country_code);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, countryName);

    spinner = (Spinner)findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(this);
    spinner.setAdapter(adapter);

    //TextDialCode.setText((CharSequence) phoneCode);



}

public String loadJSONFromAsset() {
    String json = null;
    try {
        InputStream is = getAssets().open("country_phones.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}


public void onItemSelected(AdapterView<?> parent, View view, int position,
                           long id) {
    Toast.makeText(LoginActivity.this, parent.getItemAtPosition(position).toString() + " Selected" , Toast.LENGTH_LONG).show();
    Country = parent.getItemAtPosition(position).toString();

    String Text = String.valueOf(spinner.getSelectedItem());
    String catID = String.valueOf(spinner.getSelectedItemId() + 1);
    Toast.makeText(LoginActivity.this, Text + catID , Toast.LENGTH_LONG).show();

    TextDialCode.setText(catID);
}

Some of my Json data

{
    "countries": [
  {
    "name": "Afghanistan",
    "dial_code": "+93",
    "code": "AF"
  },
  {
    "name": "Albania",
    "dial_code": "+355",
    "code": "AL"
  },
  {
    "name": "Algeria",
    "dial_code": "+213",
    "code": "DZ"
  },
  {
    "name": "AmericanSamoa",
    "dial_code": "+1 684",
    "code": "AS"
  },
  {
    "name": "Andorra",
    "dial_code": "+376",
    "code": "AD"
  },

Please i will love to know how to set name to TextCountry, code to TextCode and dial_code to TextDialCode.

Thanks

Upvotes: 1

Views: 66

Answers (1)

user7197369
user7197369

Reputation:

I was able to solve it by just trial and error. i had to create a model so that the values will be store there then fetched later on. this is the new code.

            ArrayList<String> countryName = new ArrayList<String>();

    json_string = loadJSONFromAsset();

    {
        // Locate the WorldPopulation Class
        world = new ArrayList<SignUp>();

        // Create an array to populate the spinner
        worldlist = new ArrayList<String>();



        try {
            // JSON file Assert Folder
            jsonobject = new JSONObject(json_string);
            // Locate the NodeList name
            jsonarray = jsonobject.getJSONArray("countries");
            for (int i = 0; i < jsonarray.length(); i++) {
                jsonobject = jsonarray.getJSONObject(i);

                SignUp worldpop = new SignUp();
                worldpop.setCountry(jsonobject.optString("name"));
                worldpop.setCountry_phone_Code(jsonobject.optString("dial_code"));
                worldpop.setCountry_Code(jsonobject.optString("code"));
                world.add(worldpop);
                // Populate spinner with country names
                worldlist.add(jsonobject.optString("name"));

            }
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, worldlist);

    spinner = (Spinner)findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(this);
    spinner.setAdapter(adapter);

 }

  public void onItemSelected(AdapterView<?> parent, View view, int position,
                           long id) {


    String CountryPhone = world.get(position).getCountry_phone_Code();
    TextDialCode.setText(CountryPhone);

    country = world.get(position).getCountry();

}

this is my model

public class SignUp {

public String country;
public String country_code;
public String country_phone_code;

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}


public String getCountry_Code() {
    return country_code;
}

public String getCountry_phone_Code() {
    return country_phone_code;
}


public void setCountry_Code(String country_code) {
    this.country_code = country_code;
}

public void setCountry_phone_Code(String country_phone_code) {
    this.country_phone_code = country_phone_code;
}


}

Upvotes: 1

Related Questions