I've created a custom AutocompleteAdapter which I need to allow the user to select a certain object (Customer).
My AutoCompleteAdapter looks like this:
package nl.raakict.android.spc.Adapter;
import android.content.Context;
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 java.util.ArrayList;
import nl.raakict.android.spc.Fragment.ZoekenFragment;
import nl.raakict.android.spc.Model.Customer;
import nl.raakict.android.spc.Model.Supplier;
import nl.raakict.android.spc.R;
public class CustomerAutocompleteAdapter extends ArrayAdapter<Customer> implements Filterable{
// Your sent context
private Context context;
// Your custom values for the spinner (User)
private ArrayList<Customer> values;
private ArrayList<Customer> originalValues;
private ArrayFilter mFilter;
private Customer selectedCustomer;
private ZoekenFragment zoekenFragment;
public CustomerAutocompleteAdapter(Context context, int textViewResourceId,
ArrayList<Customer> values) {
super(context, textViewResourceId, values);
this.context = context;
this.values = new ArrayList<Customer>(values);
this.originalValues = new ArrayList<Customer>(values);
}
public Customer getSelectedCustomer() {
return selectedCustomer;
}
public int getCount(){
return values.size();
}
public Customer getItem(int position){
return values.get(position);
}
public long getItemId(int position){
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.simple_spinner_item, null);
}
Customer item = getItem(position);
if (item!= null) {
// My layout has only one TextView
TextView itemView = (TextView) view;
if (itemView != null) {
// do whatever you want with your string and long
itemView.setText(item.toString());
}
view.setTag(item);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectedCustomer = (Customer)view.getTag();
zoekenFragment.startSearch(selectedCustomer);
}
});
}
return view;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.simple_spinner_item, null);
}
view.setTag(values.get(position));
// My layout has only one TextView
TextView label = (TextView) view;
label.setText(values.get(position).getName());
return label;
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
public void setSelectedListener(ZoekenFragment zoekenFragment) {
this.zoekenFragment = zoekenFragment;
}
private class ArrayFilter extends Filter {
private Object lock;
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (originalValues == null) {
synchronized (lock) {
originalValues =new ArrayList<Customer>(values);
}
}
if (prefix == null || prefix.length() == 0) {
synchronized (lock) {
ArrayList<Customer> list = new ArrayList<Customer>(originalValues);
results.values = list;
results.count = list.size();
}
} else {
final String prefixString = prefix.toString().toLowerCase();
ArrayList<Customer> values = originalValues;
int count = values.size();
ArrayList<Customer> newValues = new ArrayList<Customer>(count);
for (int i = 0; i < count; i++) {
Customer item = values.get(i);
if (item.getName().toLowerCase().contains(prefixString.toLowerCase())) {
newValues.add(item);
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results.values!=null){
values = (ArrayList<Customer>) results.values;
}else{
values = new ArrayList<Customer>();
}
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
This code works, selecting a customer name automatically fires the OnClickListener, but the problem is that now the dropdown list (Spinner) doesn't close and it doesn't autocomplete the name. What I want is that either the original onClick gets fired or that I do whatever the original onClick did.
Here's the onClick method I need:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectedCustomer = (Customer)view.getTag();
zoekenFragment.startSearch(selectedCustomer);
}
});
EDIT
This didn't have any positive results either:
private void initiateCustomerAutocomplete() {
customerAutocomplete = (AutoCompleteTextView) getView().findViewById(
R.id.autocomplete_klantnaam);
customerAdapter = new CustomerAutocompleteAdapter(getView()
.getContext(), android.R.layout.simple_spinner_item,
customers
);
customerAutocomplete.addTextChangedListener(this);
customerAutocomplete.setAdapter(customerAdapter);
customerAutocomplete.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Customer selectedCustomer = (Customer)view.getTag();
startSearch(selectedCustomer);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
customerAutocomplete.setThreshold(1);
customerAdapter.setSelectedListener(this);
}
So I found the solution. I looked into the AutoCompleteTextView class to see which methods he had and found the dismissDropDown() method. Next I passed my CustomAutoCompleteTextView to the adapter and changed the onClickListener to:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectedCustomer = (Customer) view.getTag();
zoekenFragment.startSearch(selectedCustomer);
customerAutocomplete.setText(selectedCustomer.getName());
customerAutocomplete.dismissDropDown();
}
});