首页 > 解决方案 > Android 编码将 Json 值填充到 Spinner 获取所有选定的值到 TextView 或 Edit

问题描述

嗨问候和问候。我正在尝试开发一个 android 应用程序,该应用程序在注册期间从存储在资产文件夹中的 Json 中获取数据并将其填充到微调器中。在代码中,我有一个微调器和多个 EditView 和 TextView。在注册中,用户从微调器中选择国家,它应该自动为 TextView 或 EditView 设置一些值,如国家代码、国家电话代码、id 等,但我发现很难这样做,已经搜索但大多数人只有两个值像 id 和 name 而我的有更多。正在修改的代码是我几周前从这个网站的某个地方得到的。

我将此添加到代码中

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

还有这个

   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);
}

我的一些 Json 数据

{
    "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"
  },

请我很想知道如何将名称设置为 TextCountry,将代码设置为 TextCode,将 dial_code 设置为 TextDialCode。

谢谢

标签: javajson

解决方案


我能够通过反复试验来解决它。我必须创建一个模型,以便将值存储在那里,然后稍后再获取。这是新代码。

            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();

}

这是我的模型

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;
}


}

推荐阅读