android无法单击AutoCompleteTextView下拉项

时间:2015-04-20 05:37:29

标签: android autocompletetextview

我有一个简单的自动完成文本视图,我试图动态访问谷歌位置名称,当我在下拉列表中获取名称,然后我无法点击/选择该下拉项目。

这是我的AutoComleteTextView

        <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/match_parent" >

        <AutoCompleteTextView
            android:id="@+id/demo_google_address_search"
            style="@style/match_width"
            android:ems="10"
            android:hint=" Enter Keywords" >

            <requestFocus />
        </AutoCompleteTextView>

    </FrameLayout>

这是活动:

        package com.ktb.gopharma.maps;

        import java.util.ArrayList;

        import android.app.Activity;
        import android.content.Context;
        import android.location.Address;
        import android.os.Bundle;
        import android.widget.AutoCompleteTextView;

        import com.ktb.gopharma.R;
        import com.ktb.gopharma.adapters.LocationAutoCompleteAdaptor;

        public class DemoGoogleMapSearching extends Activity
        {
            ArrayList<Address> addresses;
            AutoCompleteTextView addressAutoComplete;
            String characters="";
            LocationAutoCompleteAdaptor locationAutoCompleteadapter;
            Context context;

            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.demo_auto_complete);

                context=getApplicationContext();
                addressAutoComplete=(AutoCompleteTextView) findViewById(R.id.demo_google_address_search);

                addresses=new ArrayList<>();
                locationAutoCompleteadapter=new LocationAutoCompleteAdaptor(context, android.R.layout.simple_dropdown_item_1line,addresses);
                addressAutoComplete.setAdapter(locationAutoCompleteadapter);
            }
        }

Andddd这是一个适配器:

        package com.ktb.gopharma.adapters;

        import java.util.ArrayList;

        import android.annotation.SuppressLint;
        import android.content.Context;
        import android.location.Address;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.ArrayAdapter;
        import android.widget.Filter;
        import android.widget.Filterable;
        import android.widget.TextView;

        import com.ktb.gopharma.R;
        import com.ktb.gopharma.utilities.GoogleMapUtilities;

        public class LocationAutoCompleteAdaptor extends ArrayAdapter<Address> implements Filterable {

            private ArrayList<Address> addressList;
            LayoutInflater inflater;
            Context context;

            public LocationAutoCompleteAdaptor(Context context, int resource,ArrayList<Address> addresses)
            {
                super(context, resource);
                this.addressList = addresses;
                inflater = LayoutInflater.from(context);
                this.context = context;
            }

            @SuppressLint({ "SimpleDateFormat", "ResourceAsColor" })
            public View getView(int position, View convertView, ViewGroup parent)
            {
                View view = convertView;

                if (view == null)
                {
                    view = inflater.inflate(R.layout.address_autocomplete_list_item,parent, false);
                }
                Address address = addressList.get(position);
                try {
                    ((TextView) view.getTag(R.id.title)).setText(address.getFeatureName());
                }
                catch (Exception e) {
                    ((TextView) view.getTag(R.id.title)).setText("No More Data");
                }
                return view;
            }

            public int getCount()
            {
                if (addressList != null)
                    return addressList.size();
                return 0;
            }

            public Address getItem(int position)
            {
                return null;
            }

            public long getItemId(int position)
            {
                return 0;
            }

            @Override
            public Filter getFilter() {
                Filter filter = new Filter() {
                    @Override
                    protected FilterResults performFiltering(CharSequence constraint)
                    {
                        FilterResults filterResults = new FilterResults();
                        if (constraint != null)
                        {
                                            Geocoder geocoder=new Geocoder(context,Locale.getDefault());
                                // Retrieve the autocomplete results.
                                ArrayList<Address> searchData = new ArrayList<>();
                                try {
                                    searchData= (ArrayList<Address>) geocoder.getFromLocationName(constraint.toString(), 10);
                                }
                                catch (IOException e) {
                                    e.printStackTrace();
                                }

                                if(searchData!=null && !searchData.isEmpty())
                                {
                                    addressList = searchData;

                                    // Assign the data to the FilterResults
                                    filterResults.values = addressList;
                                    filterResults.count = addressList.size();
                                }
                        }
                        return filterResults;
                    }

                    @Override
                    protected void publishResults(CharSequence constraint,FilterResults results)
                    {
                        if (results != null && results.count > 0)
                            notifyDataSetChanged();
                        else
                            notifyDataSetInvalidated();
                    }
                };
                return filter;
            }
        }

这是下拉项目:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp"
        android:background="@drawable/rounded_corners" >

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="no messages"
            android:textColor="@color/black"
            android:textSize="16sp" />
    </LinearLayout>

所以这是我的简单代码,我得到了所有的东西,除了我无法点击下拉列表项..请帮我解决。

1 个答案:

答案 0 :(得分:1)

我不确定您的代码到底要做什么,但我可以看到您需要更改它们两件事:

1)从适配器中删除``getItemId`方法

 public long getItemId(int position)
        {
            return 0;
        }

2)将适配器更改为getItem方法

public Address getItem(int position)
        {
            return addressList.get(position);
        }

试一试。如果它仍然不起作用,那么您的问题将在getFilter方法中。对于getFilter的正确实施,您可以遵循这个example。我希望这有帮助