Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6945379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:25:43+00:00 2026-05-27T13:25:43+00:00

I ran into a bind whereby I had to sort the data read from

  • 0

I ran into a bind whereby I had to sort the data read from the phones PIM. In doing this I lost the other to which each contact field was referenced to the telephone number because I made use of 2 separate vectors as illustrated below
Before sorting

 Nna - +445535533                       
Ex - +373773737                         
Ab - +234575757                         
After sorting.(Which shouldn't be)
 Ab - +445535533
 Ex - +373773737
 Nna  - +234575757

This gives an undesired behavior since the sort removes the index to index pointer of the vectors and a selected name (in a Multiple list Box) will get a wrong number.
Alternatively,
I used a hashtable, with the intention of using the names as keys and numbers as the values.
But this pairing means duplicate names being used as keys will not be allowed. Thus I made it a i.e the phone number as keys instead.
I don’t want to sound like a cry baby so I stop here for a while and so you the code with a hope u guys would understand it

MY QUESTION
1. Is there a better way/algorithm to implement this?
2. How do I implement the getSelectedItems() in such a ways that it grabs the numbers of the selected indexes of a MULTIPLE CHOICE LIST from a hashTable

import java.util.Enumeration;
import java.util.Vector;
import java.util.Hashtable;
import javax.microedition.lcdui.List;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMException;

/**
 *
 * @author nnanna
 */
public class LoadContacts implements Operation {

    private boolean available;
    private Vector telNames = new Vector();
    Vector telNumbers = new Vector();
    Hashtable Listcontact = new Hashtable();
    private String[] names;

    public Vector getTelNames() {
        return telNames;
    }

    public Hashtable getListcontact() {
        return Listcontact;
    }

    public void execute() {
        try {
// go through all the lists
            String[] allContactLists = PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);

            if (allContactLists.length != 0) {
                for (int i = 0; i < allContactLists.length; i++) {
                    System.out.println(allContactLists[i]);
                    System.out.println(allContactLists.length);
                    loadNames(allContactLists[i]);
                    System.out.println("Execute()");
                }

            } else {
                available = false;
            }
        } catch (PIMException e) {
            available = false;

        } catch (SecurityException e) {
            available = false;

        }
    }

    private void loadNames(String name) throws PIMException, SecurityException {
        ContactList contactList = null;

        try {
            contactList = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY, name);
            // First check that the fields we are interested in are supported(MODULARIZE)
            if (contactList.isSupportedField(Contact.FORMATTED_NAME) && contactList.isSupportedField(Contact.TEL)) {
                Enumeration items = contactList.items();
                Hashtable temp = new Hashtable();
                while (items.hasMoreElements()) {
                    Contact contact = (Contact) items.nextElement();
                    int telCount = contact.countValues(Contact.TEL);
                    int nameCount = contact.countValues(Contact.FORMATTED_NAME);
                    if (telCount > 0 && nameCount > 0) {
                        String contactName = contact.getString(Contact.FORMATTED_NAME, 0);
                        // go through all the phone availableContacts

                        for (int i = 0; i < telCount; i++) {
                            System.out.println("Read Telno");
                            int telAttributes = contact.getAttributes(Contact.TEL, i);
                            String telNumber = contact.getString(Contact.TEL, i);
                            Listcontact.put(telNumber, contactName);
                            temp.put(contactName, telNumber);
                        }
                        names = getSortedList();
//                            Listcontact = temp;
                        System.out.println(temp + "-------");
                        System.out.println(Listcontact + "*******");
                        shortenName(contactName, 20);
                    }
                    available = true;
                }
            } else {
                available = false;
            }
        } finally {
// always close it
            if (contactList != null) {
                contactList.close();
            }
        }
    }

    private void shortenName(String name, int length) {
        if (name.length() > length) {
            name = name.substring(0, 17) + "...";
        }
    }

    public Vector getSelectedItems(List lbx) {
        boolean[] arrSel = new boolean[lbx.size()];
        Vector selectedNumbers = new Vector();
        int selected = lbx.getSelectedFlags(arrSel);
        String selectedString;
        String result = "";
        for (int i = 0; i < arrSel.length; i++) {
            if (arrSel[i]) {
                selectedString = lbx.getString(lbx.getSelectedFlags(arrSel));
                result = result + " " + i;
                System.out.println(Listcontact.get(selectedString));
//                System.out.println(telNumbers.elementAt(i));
            }
        }
        return selectedNumbers;
    }

    private String[] sortResults(String data[]) {
        RecordSorter sorter = new RecordSorter();
        boolean changed = true;
        while (changed) {
            changed = false;
            for (int j = 0; j < (data.length - 1); j++) {
                String a = data[j], b = data[j + 1];
                if (a != null && b != null) {
                    int order = sorter.compare(a.getBytes(), b.getBytes());
                    if (order == RecordSorter.FOLLOWS) {
                        changed = true;
                        data[j] = b;
                        data[j + 1] = a;
                    }
                }
            }
        }
        return data;
    }

    public String[] getNames() {
        return names;
    }
        Vector elements = new Vector();
    private String[] getValueArray(Hashtable value) {

        System.out.println(Listcontact + " c");
        Enumeration e = value.elements();
        while (e.hasMoreElements()) {
            elements.addElement(e.nextElement());
        }
        String[] elementsArray = new String[elements.size()];
        elements.copyInto(elementsArray);
        elements.removeAllElements();
        System.out.println(elementsArray + " k");
        return elementsArray;
    }

    public void getDuplicates(Vector realValue) {
        Vector duplicate = new Vector();
        Enumeration e = realValue.elements();
        for (int i = 0; e.hasMoreElements(); i++) {
            if (duplicate.isEmpty() || !duplicate.elementAt(i).equals(e.nextElement())) {
                break;
            } else {
                duplicate.addElement(e.nextElement());
            }
        }
    }

    public String[] getSortedList() {
        return sortResults(getValueArray(Listcontact));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:25:44+00:00Added an answer on May 27, 2026 at 1:25 pm

    Let me reiterate you requirement: You want a method that will sort the contacts read from native phonebook, then alphabetically sort them on name.

    Following is the approach,

    Replace the vectors and hash-tables in your code with a single vector, say contactListVector, containing elements of type ContactItem, don’t worry this class is explained below. Fundamentally the contact’s name and number(s) are linked together in a ContactItem, hence you do not have to worry about there mappings which reduces the usage of redundant data structures.

    class ContactItem {
        private String name;
        private String tnumber;  //this can also be a data structure 
                                 //for storing multiple numbers
    
        ContactItem( String name, String tnumber) {
            this.name = name;
            this.tnumber = tnumber;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getTnumber() {
            return tnumber;
        }
    
        public void setTnumber(String tnumber) {
            this.tnumber = tnumber;
        }        
    }
    

    You can reuse the sorting algorithm on contactListVector by comparing the member variable ContactItem.name of the vector element. Also you can deploy different sorts on member variables numbers and/or names. Also there are lots of libraries for JavaME available that have better sorting algorithm’s implemented if need be use them.

    I would recommend you to perform the sorting once on the contactListVector elements at the end of your method loadNames(...) maybe in the finally block triggered by some boolean variable. The current sorting call in each iteration on items enumeration is expensive and time consuming.

    Also you can serialize / deserialize the ContactItem thus persist your contact list.

    Let me know if you need detailed explanation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ran into something interesting, want to know if I'm doing something wrong or if
I ran into this today when unit testing a generic dictionary. System.Collections.Generic.Dictionary<int, string> actual,
I ran into a situation where I had the following two implementations located in
Ran into this while converting a VB.NET interface to C#; the VB version defines
I ran into a slight coding problem with WordPress template. This is the code
Ran into this problem today, posting in case someone else has the same issue.
trying to integrate hibernate and spring ,I ran into this error SEVERE: Context initialization
haven't done much jquery and ran into a problem. I'd like to bind hover
I'm playing around with some HTML5 elements, and ran into a fun behavior. This
I ran into this code on Wikipedia : (define (pyth x y k) (*

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.