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.