Android 编码 填充 Json 值到微调器 获取所选的所有值到 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();

        }



    }


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


阿波罗的战车
浏览 101回答 1
1回答

胡子哥哥

我能够通过反复试验来解决它。我必须创建一个模型,以便值将存储在那里,然后稍后获取。这是新代码。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> countryName = new ArrayList<String>();&nbsp; &nbsp; json_string = loadJSONFromAsset();&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Locate the WorldPopulation Class&nbsp; &nbsp; &nbsp; &nbsp; world = new ArrayList<SignUp>();&nbsp; &nbsp; &nbsp; &nbsp; // Create an array to populate the spinner&nbsp; &nbsp; &nbsp; &nbsp; worldlist = new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // JSON file Assert Folder&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonobject = new JSONObject(json_string);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Locate the NodeList name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonarray = jsonobject.getJSONArray("countries");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < jsonarray.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonobject = jsonarray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SignUp worldpop = new SignUp();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; worldpop.setCountry(jsonobject.optString("name"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; worldpop.setCountry_phone_Code(jsonobject.optString("dial_code"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; worldpop.setCountry_Code(jsonobject.optString("code"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; world.add(worldpop);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Populate spinner with country names&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; worldlist.add(jsonobject.optString("name"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("Error", e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android.R.layout.simple_spinner_dropdown_item, worldlist);&nbsp; &nbsp; spinner = (Spinner)findViewById(R.id.spinner);&nbsp; &nbsp; spinner.setOnItemSelectedListener(this);&nbsp; &nbsp; spinner.setAdapter(adapter);&nbsp;}&nbsp; public void onItemSelected(AdapterView<?> parent, View view, int position,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;long id) {&nbsp; &nbsp; String CountryPhone = world.get(position).getCountry_phone_Code();&nbsp; &nbsp; TextDialCode.setText(CountryPhone);&nbsp; &nbsp; country = world.get(position).getCountry();}这是我的模型public class SignUp {public String country;public String country_code;public String country_phone_code;public String getCountry() {&nbsp; &nbsp; return country;}public void setCountry(String country) {&nbsp; &nbsp; this.country = country;}public String getCountry_Code() {&nbsp; &nbsp; return country_code;}public String getCountry_phone_Code() {&nbsp; &nbsp; return country_phone_code;}public void setCountry_Code(String country_code) {&nbsp; &nbsp; this.country_code = country_code;}public void setCountry_phone_Code(String country_phone_code) {&nbsp; &nbsp; this.country_phone_code = country_phone_code;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java