gpt4 book ai didi

java - Android 编码将 Json 值填充到 Spinner 获取所选 TextView 或编辑的所有值

转载 作者:行者123 更新时间:2023-11-30 05:43:06 26 4
gpt4 key购买 nike

您好,问候和问候。我正在尝试开发一个 Android 应用程序,在注册期间从存储在 Assets 文件夹中的 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。

谢谢

最佳答案

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

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


}

关于java - Android 编码将 Json 值填充到 Spinner 获取所选 TextView 或编辑的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55326556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com