gpt4 book ai didi

Android 自动完成 onclicklistener

转载 作者:行者123 更新时间:2023-12-05 00:07:21 30 4
gpt4 key购买 nike

我创建了一个自定义的 AutocompleteAdapter,我需要允许用户选择某个对象(客户)。

我的 AutoCompleteAdapter 看起来像这样:

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


}

此代码有效,选择客户名称会自动触发 OnClickListener,但问题是现在下拉列表 (Spinner) 不会关闭,也不会自动完成名称。我想要的是触发原始 onClick,或者我执行原始 onClick 所做的任何事情。

这是我需要的 onClick 方法:

view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectedCustomer = (Customer)view.getTag();
zoekenFragment.startSearch(selectedCustomer);
}
});

编辑

这也没有任何积极的结果:

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

最佳答案

所以我找到了解决方案。我查看了 AutoCompleteTextView 类以查看他有哪些方法并找到了 dismissDropDown() 方法。接下来,我将 CustomAutoCompleteTextView 传递给适配器并将 onClickListener 更改为:

         view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectedCustomer = (Customer) view.getTag();
zoekenFragment.startSearch(selectedCustomer);
customerAutocomplete.setText(selectedCustomer.getName());
customerAutocomplete.dismissDropDown();
}
});

关于Android 自动完成 onclicklistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24322884/

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