auto完成textview设置隐藏值和可见文本android

时间:2015-02-18 13:12:34

标签: android

我尝试在android中构建自动完成textview,它的工作正常,但我的问题是我需要得到一个与项目相关的值,我的意思是

我们有学校

abc,bbt,ccce,ddde

abc有id = 1,number_of_students = 30

bbt有id = 2,number_of_students = 20名学生

ccce有id = 3,number_of_students = 50名学生

ddde有id = 4,number_of_students = 40名学生

当用户在文字视图 abc 中书写并选择它时,我需要 id number_of_students ,而不仅仅是abc text,< / p>

我的意思是在HTML中我们有<option value="1">text<option>,当选择它时,我们得到1,而不是文本,在android中有选项来设置文本而我得到的值不是文本

非常感谢。

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, COUNTRIES);
    AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.schoolsAutoComp);
    textView.setAdapter(adapter);

    textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
            //pos is the position of the selected item
            Toast toast = Toast.makeText(getApplicationContext(), COUNTRIES[pos], Toast.LENGTH_LONG);
            toast.show();
        }
    });

最终解决方案:

        textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
            String selection = (String) parent.getItemAtPosition(pos);
            int pos2 = -1;

            for (int i = 0; i < COUNTRIES.length; i++) {
                if (COUNTRIES[i].equals(selection)) {
                    pos2 = i;
                    break;
                }
            }
            System.out.println("Position " + pos2); //check it now in Logcat
        }
    });

1 个答案:

答案 0 :(得分:1)

setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id)
      {
         //pos is the position of the selected item
      }
    });