G
G
gamess4312021-11-22 23:20:42
Java
gamess431, 2021-11-22 23:20:42

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"
    }...
  ]
}


java code
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();
        }


an adapter that passes data to the ListView
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);

result!

87lys.jpg

The code works, but not quite correctly.

Let's get back to the questions.


  1. The list of countries in the ListView comes out in order, as written in the Json file, how to make it show in alphabetical order from A to Z?


  2. When I select a country from the list, the Editext (TextInputLayout)entire object is returned {Unicode=, Dial=+7, Name=Russia}. How can I display only the name of the selected country?


  3. How to get the country automatically in the Country Selection field when the user enters a country code in the Country Code field and vice versa?



Er49Z.jpg

Any help is welcome.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2021-11-23
Hasanly @azerphoenix

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?

After deserializing the data, sort the data
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

Well, it means that you either need to get the name with a getter, or you can deserialize into another object, in which there is only a name.
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!?

If I understand you correctly, then you mean fuzzy search. Those. end-to-end search.

J
Jacen11, 2021-11-23
@Jacen11

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 list
where 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?
Do you even read what you write? what is this sea of ​​typos? you don't have decoders sitting here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question