com.android.inputmethod.latin中的ANR原因:输入调度超时

时间:2018-10-15 11:10:36

标签: android android-layout listview android-fragments

我在本地数据库中有近6,000个数据。在一个片段上,我从该记录中得到一个字段,即“ EAN Code”,即接近6,000。现在,我在ArrayList中设置此数据。之后,我在AutoTextCompleteView的适配器中设置该ArrayList。当我搜索AutoTextCompleteView时,过了一段时间,它挂了我的页面。我的片段没有响应。我感到困惑是由于数据过多还是什么。请帮忙。

代码:

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

                }

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

        }
    });

整个片段代码

public class SearchProductFragment extends Fragment implements View.OnClickListener {


ImageButton imgBarcodeScan;
AutoCompleteTextView actSearchCode;
RadioButton rdbArticleCode, rdbEANCode;
RadioGroup rgGroup;
ImageView imgClose,imgSourceClose;
String getCodeType = "art";
DatabaseHelper databaseHelper;

ArrayList<ModelProductDetail> modelProductArticleCodeList;
ArrayList<ModelProductDetail> modelProductCodeList;
ArrayList<ModelUnrecognisedCode> modelUnrecognisedCodeArrayList;
ArrayList<ModelProductDetail> singleProductDetailList;
ArrayList<ModelUnrecognisedCode> singleUnrecognisedCheck;


List<String> productCodeList;

private ZBarScannerView scannerView;
private LayoutInflater mInflater;
private ViewGroup mContainer;
ViewGroup placeholder;
private static final int REQUEST_CAMERA = 1;

String article_code;
Boolean unrecognised_alert;
String unrecognised_code = "";

ArrayAdapter<String> adapter;
int codeValue;

KeyEvent keyevent;

// scan code

ScanDevice sm;
private final static String SCAN_ACTION = "scan.rcv.message";
private String barcodeStr = "";

private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        byte[] barocode = intent.getByteArrayExtra("barocode");
        int barocodelen = intent.getIntExtra("length", 0);
        byte temp = intent.getByteExtra("barcodeType", (byte) 0);
        android.util.Log.i("debug", "----codetype--" + temp);
        barcodeStr = new String(barocode, 0, barocodelen);

        Log.e("TAG", " barcodeStr" + barcodeStr);


        //   showScanResult.setText(barcodeStr);
        sm.stopScan();

        codeValue = 1;

        if(! (barcodeStr.equals(""))){


            singleProductDetailList = databaseHelper.getProductByEANCode(barcodeStr);

            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", barcodeStr);
                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(barcodeStr);


                Vibrator vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
                vibrator.vibrate(1000);

                MediaPlayer mp = MediaPlayer.create(context, 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();
                }


            }

        }




    }

};



@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_search_product, container, false);

    mInflater = inflater;
    mContainer = container;

    getIntializedID(v);
    placeholder = (ViewGroup) v;

    return placeholder;

}


private void getIntializedID(View view) {

    //scan device code
     sm = new ScanDevice();
    //

    imgBarcodeScan = (ImageButton) view.findViewById(R.id.imgBarcodeScan);
    rgGroup = (RadioGroup) view.findViewById(R.id.rgGroup);
    actSearchCode = (AutoCompleteTextView) view.findViewById(R.id.actSearchCode);
    imgClose = (ImageView) view.findViewById(R.id.imgClose);
    imgSourceClose = (ImageView) view.findViewById(R.id.imgSourceClose);

    databaseHelper = new DatabaseHelper(getActivity());
    modelProductCodeList = new ArrayList<>();
    productCodeList = new ArrayList<>();
    singleProductDetailList = new ArrayList<>();

    imgBarcodeScan.setOnClickListener(this);
    imgClose.setOnClickListener(this);

    // new chnage in code
    actSearchCode.setText("");
    actSearchCode.setAdapter(null);



    actSearchCode.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            //do nothing

            if(actSearchCode.getText().length() == 0){

                if(getCodeType.equals("ean")){

                    actSearchCode.setText("871128");

                }
                else{
                    actSearchCode.setText("");
                }

            }

        }

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

            }
        }
    });




    adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, android.R.id.text1, productCodeList);
    actSearchCode.setAdapter(adapter);




    // sample code barcode



}

public void changeFragments(Fragment fragment) {
    FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

@Override
public void onClick(View view) {


    if (view.getId() == R.id.imgBarcodeScan) {

        //MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.beep);
        // mp.start();
        //scanCode(view);
       // onKeyDown(301, keyevent);

     Intent in = new Intent(getActivity(), ScanActivity.class);
     startActivity(in);
    }

    else if(view.getId() == R.id.imgClose){

        if(getCodeType.equals("ean")){

            actSearchCode.setText("871128");
        }
        else {
            actSearchCode.setText("");
        }
    }


}


@Override
public void startActivityForResult(Intent intent, int requestCode) {
    super.startActivityForResult(intent, requestCode);

}


private void getDialog() {

    final Dialog d = new Dialog(getActivity());
    d.setContentView(R.layout.dialog_unrecognised_code);
    d.setCanceledOnTouchOutside(false);
    d.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

    final EditText etCode = (EditText) d.findViewById(R.id.etCode);
    final EditText etrecognisedComment = (EditText) d.findViewById(R.id.etrecognisedComment);
    ImageView imgClose = (ImageView) d.findViewById(R.id.imgClose);
    ImageButton ibSubmit = (ImageButton) d.findViewById(R.id.ibSubmit);

    if(codeValue == 1){
        etCode.setText(barcodeStr);
    }else if(codeValue == 2) {
        etCode.setText(unrecognised_code);

    }

    imgClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            KEY_UNRECOGNISED_CODE = "";
            d.dismiss();
        }
    });


    ibSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            SessionManager session = new SessionManager(getContext().getApplicationContext());
            HashMap<String, String> user = session.getSelectedShopDetail();

            ModelUnrecognisedCode modelUnrecognisedCode = new ModelUnrecognisedCode();
            modelUnrecognisedCode.setUnrecognised_code(etCode.getText().toString());
            modelUnrecognisedCode.setUnrecognised_comment(etrecognisedComment.getText().toString());
            modelUnrecognisedCode.setShop_id(user.get("shop_id"));
            modelUnrecognisedCode.setShop_name_fr(user.get("shop_name_fr"));
            modelUnrecognisedCode.setShop_name_nl(user.get("shop_name_nl"));

            databaseHelper.addUnrecognisedCode(modelUnrecognisedCode);

            // changeFragments(new SearchProductFragment());

            modelUnrecognisedCodeArrayList = new ArrayList<>();
            modelUnrecognisedCodeArrayList = databaseHelper.getUnrecognisedCode();

            Toast.makeText(getActivity(), R.string.unrecognised_code_added_in_cart,Toast.LENGTH_SHORT).show();

            KEY_UNRECOGNISED_CODE = "";
            d.dismiss();

        }
    });


    d.show();


}



public void onKeyDown(int keyCode, KeyEvent event) {

    int charCode = event.getKeyCode();

    System.out.println("charCode = " + keyCode +"  "+event);
    //sm.openScan();

    if (charCode == 302 || charCode == 301 || charCode == 303) {

        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
                Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(actSearchCode.getApplicationWindowToken(), 0);

        System.out.println("openScannerL = " + sm.getOutScanMode());

        sm.openScan();

    } else if (charCode == 82) {

    } else {
        sm.closeScan();
    }

}


@Override
public void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    if (sm != null) {
        sm.stopScan();
    }
    getActivity().unregisterReceiver(mScanReceiver);
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();


    IntentFilter filter = new IntentFilter();
    filter.addAction(SCAN_ACTION);
    getActivity().registerReceiver(mScanReceiver, filter);



    // article code start

    modelProductCodeList.clear();
    productCodeList.clear();


    if (getCodeType.equals("art")) {

        modelProductCodeList.clear();
        productCodeList.clear();


        modelProductCodeList = databaseHelper.getProductsArticleCode();

        for (int k = 0; k < modelProductCodeList.size(); k++) {
            productCodeList.add(modelProductCodeList.get(k).getArticle_code());

        }


    }


    rgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {


            if (i == R.id.rdbArticleCode) {


                actSearchCode.setText("");
                getCodeType = "art";

                modelProductCodeList.clear();
                productCodeList.clear();


                modelProductCodeList = databaseHelper.getProductsArticleCode();

                databaseHelper.close();


                for (int k = 0; k < modelProductCodeList.size(); k++) {
                    productCodeList.add(modelProductCodeList.get(k).getArticle_code());
                }


              adapter.notifyDataSetChanged();


            } else if (i == R.id.rdbEANCode) {

                actSearchCode.setText("871128");

                getCodeType = "ean";

                modelProductCodeList.clear();
                productCodeList.clear();

                modelProductCodeList = databaseHelper.getProductsEANCode();
                databaseHelper.close();

                for (int k = 0; k < modelProductCodeList.size(); k++) {
                    productCodeList.add(modelProductCodeList.get(k).getEan_code());

                }

               adapter.notifyDataSetChanged();

            }

        }
    });


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

        }
    });



    //end




    if(!(KEY_UNRECOGNISED_CODE.equals(""))) {



        if(!(KEY_SOURCE_CODE.equals(""))){

            Fragment fragment = new ProductDetailFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); // this is basically context of the class
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Bundle bundle = new Bundle();

            bundle.putString("product_art",KEY_UNRECOGNISED_CODE);
            bundle.putString("source_type", KEY_SOURCE_CODE);

            fragment.setArguments(bundle);
            fragmentTransaction.replace(R.id.frame, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

            KEY_SOURCE_CODE = "";
            KEY_UNRECOGNISED_CODE = "";

        }
        else {
            unrecognised_code = KEY_UNRECOGNISED_CODE;

            codeValue = 2;
            getDialog();
        }
    }


    if (getArguments() != null) {

        unrecognised_alert = getArguments().getBoolean("unrecognised_alert");

    }

}


@Override
public void onDestroyView() {
    super.onDestroyView();
}

}

2 个答案:

答案 0 :(得分:0)

您应该在初始化所有变量后将它们添加到onCreate中

adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, android.R.id.text1, productCodeList);
    actSearchCode.setAdapter(adapter);

,而且您不需要每次都只需要设置适配器一次就设置新适配器,然后使用productCodeList.clear()并添加所有对象,然后调用 adapter.notifyDatasetChanged()

答案 1 :(得分:0)

最好使用其他ArrayList而不是单个ArrayList

  HashMap<String,ArrayList> mapSearchList= new LinkedHashMap<>();

在ontextchange侦听器中,如果您从第一个字符开始设置为适配器

   setadapter(context,mapSearchList.get("a")) // etc

并设置阈值