Answer the question
In order to leave comments, you need to log in
3 questions about working with json in java, how to solve this problem?
I have several difficulties that I could not solve on my own, first things first.
There is a json file that I show in ListView,
and it looks like this:
countryData.json
{
"CountryData": [
{
"Name": "Bangladesh",
"Unicode": "",
"Dial": "880"
},
{
"Name": "Belgium",
"Unicode": "",
"Dial": "32"
}...
]
}
ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
JSONObject jsonObject;
JSONArray jsonArray;
String jsonFile;
String unicode;
String name;
String dial;
try {
InputStream stream = getAssets().open("json/countryData.json");
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
jsonFile = new String(buffer);
jsonObject = new JSONObject(jsonFile);
jsonArray = jsonObject.getJSONArray("CountryData");
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
unicode = jsonObject.getString("Unicode");
name = jsonObject.getString("Name");
dial = jsonObject.getString("Dial");
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("Unicode", unicode);
hashMap.put("Name", name);
hashMap.put("Dial", "+"+dial);
arrayList.add(hashMap);
}
} catch (JSONException | IOException e) {
Toast.makeText(getApplicationContext(), "json file is not found", Toast.LENGTH_LONG).show();
}
adapter = new SimpleAdapter(
LoginActivity.this,
arrayList,
R.layout.simple_layout,
new String[]{"Unicode", "Name", "Dial"},
new int[]{R.id.countryUnicode,R.id.countryName, R.id.countryCodeList}
);
selectCountry.setAdapter(adapter);
Editext (TextInputLayout)
entire object is returned {Unicode=, Dial=+7, Name=Russia}
. How can I display only the name of the selected country?Answer the question
In order to leave comments, you need to log in
if you notice the list of countries in the ListView come out in order as it is written in the Json file, so how to make it show in alphabetical order from A to Z?
when I select some country from the list, then in Editext (TextInputLayout) it returns the entire object, and I need to display only the names of the selected country
how do i get the country automatically in the country selection fields when the user enters the country code in the fields where the country code is entered and vice versa!?
Never thought I'd ever say this, but learn data structures ! Why the hell are you making a hashmap list with three elements!
Now for your questions
the list of countries in the ListView comes out in order as it is written in the Json file, so how to make it show in alphabetical order from A to Z?obviously you need to sort this list first
when I select some country from the listwhere is the code that does this? Although the answer will already be obvious, pass only the country, not all
how do i get the country automatically in the country selection fields when the user enters the country code in the fields where the country code is entered and vice versa!?you even have all the data for this, you just take it and substitute it, what's the problem?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question