我的应用程序中有一个autoCompleteTextView,当用户完成键入时,检查其afterTextChanged方法,并根据用户输入的代码将用户重定向到下一个详细信息页面,但是问题是,当用户从详细信息页面单击返回时,它会再次切换进入详细页面。 afterTextChanged方法每次都调用。我在onCreatedView方法中调用了它。我该如何预防。请帮忙。
代码:
actSearchCode.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
//do nothing
String product_code = actSearchCode.getText().toString();
Log.e("TAG", "afterTextChanged: " );
if(getCodeType.equals("art")) {
if (actSearchCode.getText().length() != 0 && actSearchCode.getText().length() >= 5) {
singleProductDetailList = databaseHelper.getProductByArtCode(product_code);
if (singleProductDetailList.size() != 0) {
Fragment fragment = new ProductDetailFragment();
FragmentManager fragmentManager = getFragmentManager(); // this is basically context of the class
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("product_art", product_code);
bundle.putString("source_type", "ean");
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else {
singleUnrecognisedCheck = new ArrayList<>();
singleUnrecognisedCheck = databaseHelper.getUnrecognisedByCode(product_code);
Vibrator vibrator = (Vibrator) getActivity().getSystemService(getActivity().VIBRATOR_SERVICE);
vibrator.vibrate(1000);
MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.beep);
mp.start();
if (singleUnrecognisedCheck.size() == 0) {
getDialog();
} else {
Toast.makeText(getActivity(), R.string.unrecognised_code_has_been_added, Toast.LENGTH_SHORT).show();
}
}
}
}
else{
if (actSearchCode.getText().length() != 0 && actSearchCode.getText().length() >= 13) {
singleProductDetailList = databaseHelper.getProductByArtCode(product_code);
if (singleProductDetailList.size() != 0) {
Fragment fragment = new ProductDetailFragment();
FragmentManager fragmentManager = getFragmentManager(); // this is basically context of the class
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("product_art", product_code);
bundle.putString("source_type", "ean");
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else {
singleUnrecognisedCheck = new ArrayList<>();
singleUnrecognisedCheck = databaseHelper.getUnrecognisedByCode(product_code);
Vibrator vibrator = (Vibrator) getActivity().getSystemService(getActivity().VIBRATOR_SERVICE);
vibrator.vibrate(1000);
MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.beep);
mp.start();
if (singleUnrecognisedCheck.size() == 0) {
codeValue = 1;
getDialog();
} else {
Toast.makeText(getActivity(), R.string.unrecognised_code_has_been_added, Toast.LENGTH_SHORT).show();
}
}
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//do nothing
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() != 0) {
imgClose.setVisibility(View.VISIBLE);
imgSourceClose.setVisibility(View.GONE);
} else {
imgClose.setVisibility(View.GONE);
imgSourceClose.setVisibility(View.VISIBLE);
}
}
});
// article code start
modelProductCodeList.clear();
productCodeList.clear();
if (getCodeType.equals("art")) {
modelProductCodeList = databaseHelper.getProductsArticleCode();
for (int k = 0; k < modelProductCodeList.size(); k++) {
productCodeList.add( modelProductCodeList.get(k).getArticle_code());
}
actSearchCode.setAdapter(null);
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, productCodeList);
actSearchCode.setAdapter(adapter);
}
rgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (i == R.id.rdbArticleCode) {
actSearchCode.setText("");
getCodeType = "art";
modelProductCodeList = databaseHelper.getProductsArticleCode();
databaseHelper.close();
productCodeList.clear();
for (int k = 0; k < modelProductCodeList.size(); k++) {
productCodeList.add(modelProductCodeList.get(k).getArticle_code());
}
actSearchCode.setAdapter(null);
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, productCodeList);
actSearchCode.setAdapter(adapter);
} else if (i == R.id.rdbEANCode) {
actSearchCode.setText("871128");
getCodeType = "ean";
modelProductCodeList = databaseHelper.getProductsEANCode();
databaseHelper.close();
productCodeList.clear();
for (int k = 0; k < modelProductCodeList.size(); k++) {
productCodeList.add(modelProductCodeList.get(k).getEan_code());
Log.e("TAG", "onCheckedChanged: "+modelProductCodeList.get(k).getEan_code() );
}
actSearchCode.setAdapter(null);
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, productCodeList);
actSearchCode.setAdapter(adapter);
}
}
});
actSearchCode.setThreshold(3);
actSearchCode.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actSearchCode.getApplicationWindowToken(), 0);
Fragment fragment = new ProductDetailFragment();
FragmentManager fragmentManager = getFragmentManager(); // this is basically context of the class
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("product_art", (String) adapterView.getItemAtPosition(i) );
bundle.putString("source_type", getCodeType);
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
答案 0 :(得分:2)
final static boolean flag=false;//define this in the class before on create/on createView
----------
actSearchCode.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
//do nothing
if(!flag)
{
Log.e("TAG", "afterTextChanged: " );
if(getCodeType.equals("art")) {
if (actSearchCode.getText().length() != 0 && actSearchCode.getText().length() >= 5) {
singleProductDetailList = databaseHelper.getProductByArtCode(product_code);
if (singleProductDetailList.size() != 0) {
flag=true;
Fragment fragment = new ProductDetailFragment();
FragmentManager fragmentManager = getFragmentManager(); // this is basically context of the class
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("product_art", product_code);
bundle.putString("source_type", "ean");
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}else{
flag=false;
}