gpt4 book ai didi

android - AutoCompleteTextView 函数 afterTextChanged 每次调用

转载 作者:搜寻专家 更新时间:2023-11-01 09:23:03 26 4
gpt4 key购买 nike

我的应用程序中有一个 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();

}
});

最佳答案

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

关于android - AutoCompleteTextView 函数 afterTextChanged 每次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53042682/

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