Я добавил AutoCompleteTextView в свое приложение.В котором я кодировал оба метода, то есть afterTextChange (), если человек пишет полное слово и не нажимает на предложение, и OnItemClickListener (), если пользователь нажимает на sugesstion, но проблема в том, что оба эти метода работают вместе.Если я нажимаю на предложения, используя onItemClick, то afterTextChange () также работает, и мой код выполняется два раза.Что нужно сделать, чтобы оба метода не запускались одновременно.
OnAfterTextChange () Код
actvShop_name.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
actvShop_name.setVisibility(View.VISIBLE);
txtShop.setVisibility(View.GONE);
//do nothing
if ((actvShop_name.getText().length() != 0) && actvShop_name.getText().length() >= 3) {
modelShopDetailsArrayList = databaseHelper.getShopDetail(actvShop_name.getText().toString(), language);
Log.e("TAG", "onKeyDown:Add " + modelShopDetailsArrayList.size() + " " + actvShop_name.getText().toString() + language);
if (modelShopDetailsArrayList.size() != 0) {
HashMap<String, String> shopDetails = session.getSelectedShopDetail();
Log.e("TAG", "onItemClick: " + shopDetails.get("shop_name_nl") + " ");
if (shop_name != null) {
if (shop_name == actvShop_name.getText().toString()) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);
fillShopDetails(actvShop_name.getText().toString());
} else {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setMessage(R.string.delete_cart);
alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
databaseHelper.deleteCart();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);
fillShopDetails(actvShop_name.getText().toString());
}
});
alertDialog.setNegativeButton(R.string.no
, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
} else {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);
fillShopDetails(actvShop_name.getText().toString());
}
OnItemClickListener () Код
actvShop_name.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// actvShop_name.removeTextChangedListener(textWatcher);
shop_name_entered = adapterView.getItemAtPosition(i).toString();
actvShop_name.setVisibility(View.GONE);
txtShop.setVisibility(View.VISIBLE);
txtShop.setText(shop_name_entered);
HashMap<String, String> shopDetails = session.getSelectedShopDetail();
Log.e("TAG", "onItemClick: " + shopDetails.get("shop_name_nl") + " " + shop_name_entered);
if (shop_name != null) {
if (shop_name.equals(shop_name_entered)) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);
fillShopDetails(shop_name_entered);
} else {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setMessage(R.string.delete_cart);
alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
databaseHelper.deleteCart();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);
fillShopDetails(shop_name_entered);
}
});
alertDialog.setNegativeButton(R.string.no
, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
} else {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);
fillShopDetails(shop_name_entered);
}
}
});
actvShop_name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
actvShop_name.setVisibility(View.VISIBLE);
txtShop.setVisibility(View.GONE);
}
});
txtShop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
actvShop_name.setVisibility(View.VISIBLE);
txtShop.setVisibility(View.GONE);
}
});
Диалог открывается два раза, когда оба метода работают вместе.