gpt4 book ai didi

java - OnItemClickListener 无法与 AutoCompleteTextView 一起使用

转载 作者:行者123 更新时间:2023-12-02 01:05:05 25 4
gpt4 key购买 nike

我使用了带有 customLayout 和 Adapter 的 autoCompleteTextView,但问题是当我们单击建议中的某个项目时,该项目值既不会填充到 autoCompleteTextView 中,也不会触发 OnItemClickListener。

代码使用 autoCompleteTextView 中输入的文本过滤建议,但 onClick 事件没有任何反应。

下面是我的代码

TeamSuggestionAdapter.java 文件

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();

}
});

最佳答案

android:descendantFocusability="blocksDescendants" 添加到 add_match_suggestions_layout.xml 中的 LinearLayout 并删除了focusableclickable 由此,解决了问题。

关于java - OnItemClickListener 无法与 AutoCompleteTextView 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57739616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com