java - 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。
谢谢
解决方案
我能够通过反复试验来解决它。我必须创建一个模型,以便将值存储在那里,然后稍后再获取。这是新代码。
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;
}
}
推荐阅读
- twitter-bootstrap - 引导升级到最新版本
- r - 为 R plotly scatterplot 进行正确的可视化
- reactjs - 使用打字稿将自定义道具传递给反应组件
- postgresql - 是否可以停止 AWS Postgres Aurora 全球数据库?
- php - 每次在管理页面编辑帖子时,如何防止上传图片?
- java - 有没有办法将 VisualVM 附加到由 AWS SAM local 运行的 java lambda 函数
- java - Reactor - 如何将找到和未找到的实体收集到地图中,以 id 为键?
- c# - WPF - 设置字体大小以适合文本框
- ruby-on-rails - RSpec 请求测试
- typescript - 异步函数错误:“键入'Promise
[]' 不可分配给类型 'MyType[]' "