Элементы в HashMap не в порядке - PullRequest
0 голосов
/ 19 апреля 2020

Я заполняю значения в Hashmap из SQLiteDatabase и показываю их в Custom Listview с помощью Simple Adapter. Цель состоит в том, чтобы при щелчке элемента он переходил к другой операции и отображал детали элемента.

public void custNameContact() {
        custDatabase = new CustomerDatabase(this); //It is object of class which extends SQLiteDatabaseHelper

        customersNameList = new ArrayList<String>(); // ArrayList for customers' Name
        customersContactList = new ArrayList<String>(); // ArrayList for customers' Contact

        HashMap<String, String> nameContact = new HashMap<>(); // HashMap in which value fetched to be pass

        Cursor cursorNameContact = custDatabase.customerNameContact();  // fetching data from database. Data is queried such that it will give data ORDERED BY name.

        if (cursorNameContact.getCount() == 0)
        {
            Toast.makeText(this, "No Entry", Toast.LENGTH_SHORT).show();
            return;
        }
        while (cursorNameContact.moveToNext())
        {
            custNameInList = cursorNameContact.getString(0); // To get customers' Name
            customersNameList.add(custNameInList);
            custContactInList = cursorNameContact.getString(1); // To get customers' Contact
            customersContactList.add(custContactInList);

            nameContact.put(custNameInList, custContactInList); // Passing values in HashMap
        }

        List<HashMap<String, String>> listItems = new ArrayList<>();

// использование SimpleAdapter для отображения значений в пользовательском макете listView

        adapter = new SimpleAdapter(this, listItems, R.layout.listview_item_subitem, new String[]{"Name", "Contact"}, new int[]{R.id.custName, R.id.custContact});

        Iterator it = nameContact.entrySet().iterator();
        while (it.hasNext())
        {
            HashMap<String, String> nameContactMap = new HashMap<>();
            Map.Entry pair = (Map.Entry) it.next();
            nameContactMap.put("Name", pair.getKey().toString());
            nameContactMap.put("Contact", pair.getValue().toString());
            listItems.add(nameContactMap);
        }

        customerListView.setAdapter(adapter);

    }

public void itemInteraction() {
        // Click On Item to View Data
        customerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                custName = customersNameList.get(i);
                Intent viewData = new Intent(getApplicationContext(), ViewShirtDataActivity.class);
                startActivity(viewData);
            }
        });
       }

Возникла проблема в том, что элемент нажал, и элемент не совпадает. Похоже, что элементы в ArrayList customerNameList - это ORDERD по имени, но не элементы в Hashamap.

Что я могу сделать? Пожалуйста, предложите мне, если есть лучший способ сделать это

...