显示值,但从下拉列表中选择一个id

时间:2014-09-19 02:21:25

标签: java android autocomplete

  1. 用户在自动填充文本框中键入2个字母
  2. 这两个字母被保存并用于Web服务方法中 检索以这两个字母开头的所有用户
  3. 返回XML结果,并进行解析,然后检索用户名+ id和 将每个保存在不同的ArrayList
  4. 来自名字arraylist get的结果放入一个下拉列表(自动完成列表)
  5. 用户从下拉列表项
  6. 中选择一个项目

    - 我需要在下拉列表中显示名称,但是,当用户选择名称时,应选择该用户ID并将其另存为String以便用于其他查询。

    问题是:如何显示名称,但选择该名称的ID

        AutoCompleteTextView assigneeInput;
        assigneeInput=(AutoCompleteTextView)
    
        findViewById(id.editassignee);
    
        assigneeInput.addTextChangedListener(new
    
        TextWatcher() {
    
            @Override
            public void onTextChanged (CharSequence s,int start, int before, int count){
                getContactsForAssignee();
            }
            @Override
            public void beforeTextChanged (CharSequence s,int start, int count, int after){
            }
            @Override
            public void afterTextChanged (Editable s){
            }
        }
    
        );
        //Textwatcher for assignee input -end
    
    }
    
        //Method to get Contacts for the assignee autocomplete - Start
        public void getContactsForAssignee() {
            //webservice call method
    
        }
    //Method to get Contacts for the assignee autocomplete - End
    
        public void receiveResults10(String result10) {
    
            try {
    
                //Dom parsing set up
    
                List<String> valSetOne = new ArrayList<String>();
                List<String> valSetTwo = new ArrayList<String>();
                ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
    
    
                for (int i = 0; i < nodesUDSObjectList.getLength(); i++) {
                    Element elementUDSObject = (Element) nodesUDSObjectList.item(i);
                    NodeList nodesAttributeList = elementUDSObject.getElementsByTagName("Attribute");
    
                    HashMap<String, String> mapp = new HashMap<String, String>();
    
    
                    for (int iA = 0; iA < nodesAttributeList.getLength(); iA++) {
                        Element elementAttribute = (Element) nodesAttributeList.item(iA);
                        //You have attribute(iA)
    
    
                        NodeList AttrNameElementList = (NodeList) elementAttribute.getElementsByTagName("AttrName");
                        String nameValue = getCharacterDataFromElement((Element) (AttrNameElementList.item(0)));
    
                        System.out.println("name" + nameValue);
    
                        NodeList AttrValueElementList = (NodeList) elementAttribute.getElementsByTagName("AttrValue");
                        String valueValue = getCharacterDataFromElement((Element) (AttrValueElementList.item(0)));
    
                        if (nameValue.equals("name")) {
                            valSetOne.add(valueValue);
                            mapp.put(COMBO_NAME, valueValue);
                        }
                        if (nameValue.equals("id")) {
                            valSetTwo.add(valueValue);
                            mapp.put(PERSISTENT_ID, valueValue);
                        }
                    }
                    menuItems.add(mapp);
                }
                AutoCompleteTextView editAssignee;
                ArrayAdapter<String> adapter;
    
                adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, valSetOne);
                editAssignee = (AutoCompleteTextView) findViewById(R.id.editassignee);
                editAssignee.setAdapter(adapter);
    
            } catch (Exception e) {
    
                e.printStackTrace();
    
    
            }
    
        }
    
        public static String getCharacterDataFromElement(Element e) {
    
        }
    
        //Beginning of method to actually save the ticket executed on click of the "save" button
        public void SaveThisIncident(View v) {
            AutoCompleteTextView editAssigneeInput = (AutoCompleteTextView) findViewById(R.id.editassignee); //receiving the users input for assignee
            String thisIsAssignee = editAssigneeInput.getText().toString();
        }
    

1 个答案:

答案 0 :(得分:0)

您需要为AutoCompleteTextView editAssignee设置itemclicklistner&amp;使用BaseAdapter代替ArrayAdapter

传递自定义对象的ArrayList,其中包含id&amp;字符串值到baseadapter。

自定义对象可以是

public class item{
String id;
String value;
}

现在onClickItem你可以同时获得id&amp;来自你的Arraylist的价值