I used an autoCompleteTextView with customLayout and Adapter, but the issue is when we click on an item in suggestions, neither the item values gets filled in autoCompleteTextView nor the OnItemClickListener get fired.
The code filters the suggestions with the text entered in autoCompleteTextView, but nothing happends onClick Event.
Below is my code
TeamSuggestionAdapter.java file
package com.material.ipladmin;
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.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class TeamsSuggestionAdapter extends ArrayAdapter<Team> implements Filterable {
private Context context;
private List<Team> allTeams;
private List<Team> teams;
private ListFilter listFilter = new ListFilter();
public TeamsSuggestionAdapter(Context context, List<Team> teams) {
super(context, 0, teams);
this.context = context;
this.allTeams = teams;
}
@Override
public int getCount() {
return allTeams.size();
}
@Override
public Team getItem(int i) {
return allTeams.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null) {
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.add_match_suggestions_layout, viewGroup, false);
}
CircleImageView logo = view.findViewById(R.id.suggestionItemlogoImage);
TextView name = view.findViewById(R.id.suggestionItemName);
LinearLayout linearLayout = view.findViewById(R.id.addMatchSuggestionsLinearLayout);
Team team = getItem(i);
Glide.with(context).load(team.logo).into(logo);
name.setText(team.teamName);
linearLayout.setTag(team.teamId);
return view;
}
@Override
public Filter getFilter() {
return listFilter;
}
class ListFilter extends Filter {
private Object lock = new Object();
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if(teams == null) {
synchronized (lock) {
teams = new ArrayList<>(allTeams);
}
}
if (prefix == null || prefix.length() <= 0) {
synchronized (lock) {
results.values = teams;
results.count = teams.size();
}
}
else
{
String searchThis = prefix.toString().toLowerCase();
List<Team> searched = new ArrayList<>();
for (int i=0; i<teams.size(); i++) {
if (teams.get(i).teamName.toLowerCase().contains(searchThis)) {
searched.add(teams.get(i));
}
}
results.values = searched;
results.count = searched.size();
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults results) {
if (results.values != null) {
allTeams = (ArrayList<Team>) results.values;
} else {
allTeams = null;
}
if (results.count > 0 ) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
@Override
public CharSequence convertResultToString(Object resultValue) {
return ((Team) resultValue).teamId;
}
}
}
add_match_suggestions_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:clickable="true"
android:focusable="true"
android:id="@+id/addMatchSuggestionsLinearLayout">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/suggestionItemlogoImage"
android:src="@drawable/default_team_logo"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:id="@+id/suggestionItemName"
android:textColor="@color/colorBlack"
android:textSize="10sp"
android:text="Suggestion Item name"/>
</LinearLayout>
MainActivity.java
TeamsSuggestionAdapter TeamsAdapter = new TeamsSuggestionAdapter(this, teams);
autoCompleteTextView.setAdapter(TeamsAdapter);
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(AddMatchActivity.this, "Item Clicked", Toast.LENGTH_SHORT).show();
}
});
Adding android:descendantFocusability="blocksDescendants"
to my LinearLayout in add_match_suggestions_layout.xml
and removed focusable
& clickable
from it, solved the problem.