【问题标题】:Android AutoCompleteTextview click not workingAndroid AutoCompleteTextview 点击不起作用
【发布时间】:2015-03-03 10:12:02
【问题描述】:

我尝试了 AutoCompleteTextView 并获取值,但我无法在 autocompletetextview 的单击事件中获取值,这是我的布局

 <AutoCompleteTextView
    android:id="@+id/atv_places"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_below="@+id/lin"
    android:layout_gravity="center"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp"
    android:layout_marginTop="10dp"
    android:background="@drawable/et_chat"
    android:drawableLeft="@drawable/searchblue"
    android:drawablePadding="4dp"
    android:dropDownVerticalOffset="5dp"
    android:dropDownWidth="match_parent"
    android:hint="Search contacts..."
    android:scrollHorizontally="true"
    android:textColor="@color/Black"
    android:textCursorDrawable="@drawable/color_cursor"
    android:textSize="16dp"
    android:visibility="gone" /> 

和我的点击事件代码来获取我通过适配器填充的值。我无法获得点击的值或无法使用 toast。

  atvPlaces = (AutoCompleteTextView) v.findViewById(R.id.atv_places);

    mTxtPhoneNo = (AutoCompleteTextView) v.findViewById(R.id.mmWhoNo);
    mAdapternew = new SimpleAdapter(getActivity(), mPeopleList,
            R.layout.row, new String[] { "Name", "Phone" }, new int[] {
                    R.id.textView1, R.id.textView2 });
    atvPlaces.setAdapter(mAdapternew);
    atvPlaces.setThreshold(1);

    atvPlaces.setOnItemClickListener(new OnItemClickListener() {

        @SuppressWarnings("unchecked")
        @Override
        public void onItemClick(AdapterView<?> av, View arg1, int index,
                long arg3) {

            Toast.makeText(getActivity(), "auto", Toast.LENGTH_LONG).show();
            Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);

            String name  = map.get("Name");
            String number = map.get("Phone");
            atvPlaces.setText(""+name+"<"+number+">");  

        }



    });

【问题讨论】:

  • 不要使用 Toast 进行调试,而是使用 Log.* 方法

标签: android autocomplete autocompletetextview simpleadapter


【解决方案1】:

我没有发现你的代码有什么问题,但下面的代码是我用你的代码制作的,它工作正常,请自行检查。

 AutoCompleteTextView atvPlaces = (AutoCompleteTextView) findViewById(R.id.atv_places);
        ArrayList<HashMap<String,String>> mPeopleList=new ArrayList<>();
        HashMap h1 = new HashMap<String, String>();
        h1.put("Name","name1");
        mPeopleList.add(h1);

        HashMap h2 = new HashMap<String, String>();
        h2.put("Name","name2");
        mPeopleList.add(h2);

        final SimpleAdapter myAdapter = new SimpleAdapter(this, mPeopleList,
                android.R.layout.simple_spinner_dropdown_item, new String[] { "Name"}, new int[] {
                android.R.id.text1});

        atvPlaces.setAdapter(myAdapter);
        atvPlaces.setThreshold(1);

        atvPlaces.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> av, View arg1, int index,
                                    long arg3) {

                Toast.makeText(MainActivity.this, "auto", Toast.LENGTH_LONG).show();
                HashMap<String, String> map = (HashMap<String, String>) av.getItemAtPosition(index);

                String name  = map.get("Name");
                String number = map.get("Phone");

            }



        });

【讨论】:

  • 感谢它现在工作正常,当我使用 android.R.layout.simple_spinner_dropdown_item 作为我的布局但当我使用我的自定义布局 R.layout.row 时它不工作
  • @sinceksaji 将 android:id="@android:id/text1" 添加到您的自定义文本视图中。我知道收到这个答案已经很晚了,但是这些天我一直在做同样的事情,并且单击该项目不起作用,因为我的自定义 xml 布局中缺少视图的 ID。
猜你喜欢
  • 1970-01-01
  • 2011-03-26
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-04
相关资源
最近更新 更多