0

I am filling up values in Hashmap from SQLiteDatabase and showing them in Custom Listview using Simple Adapter. The goal is that when Item is clicked it will move to other activity and show the detail of the item.

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<>();

// using SimpleAdapter to show values in custom listView layout

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

Problem occuring is that item Clicked and item get is not same. It looks like items in ArrayList customerNameList is ORDERD by name but not the ITEMS in the Hashamap.

What can I do? Please suggest me if there is better method to do so

SanketG
  • 9
  • 3

0 Answers0