您好,问候和称呼。我正在尝试开发一个Android应用程序,该应用程序会在注册期间从存储在资产文件夹中的Json提取数据并将其填充到微调器中。在代码中,我有一个微调器和多个EditView和TextView。在注册过程中,用户从微调器中选择国家,它应该自动为TextView或EditView设置一些值,例如国家代码,国家电话代码,ID等,但发现这样做很困难,已经搜索过,但是大多数人只有两个值例如ID和名称,而我的则更多。几周前,我正在从该站点的某个地方修改代码。
我将此添加到了代码
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。
谢谢
答案 0 :(得分:0)
我能够通过反复试验解决它。我必须创建一个模型,以便将值存储在那里,然后在以后获取。这是新代码。
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;
}
}