2

I have a simple auto-complete text view and Im trying to access google location names dynamically,When I get names in dropdownlist,then I unable to click/select that dropdown item.

this is my 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>

This Is Activity:

        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 this an adapter:

        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;
            }
        }

and this is drop down item :

    <?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>

So this is my simple code and Im getting all things well except I unable to click on dropdown list item.. Please help me t resolve.

Vikas Kumbhar
  • 121
  • 12

1 Answers1

2

I' not sure what exactly your code is trying to do, but I can see that there are two things you need to change them:

1)Remove from the adapter the ``getItemId` method

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

2)Change in your adapter this getItem method to be

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

And give it a try. If it is still not working, then the your problem will be inside the getFilter method. This is a working example you can follow for the right implementation for getFilter. I hope this helps

Community
  • 1
  • 1
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
  • 1
    U r brilliant...Thank u very much.................By doing these 2 changes My problem get resolved....shaaaaa what a cheep problem I had??? :P – Vikas Kumbhar Apr 20 '15 at 06:02
  • @VikasKumbhar you are welcome. Don't forget to accept it as the answer by clicking on the Right sign and vote it up if you found it useful by clicking on the the up arrow. Wish you happy coding :) – Sami Eltamawy Apr 20 '15 at 06:09