按自定义 ArrayAdapter 中的输入过滤数据显示空项

Filter data by input in custom ArrayAdapter displays empty items

我实现了 EditText 小部件以使用自定义 ArrayAdapter 过滤 ListFragment 中的项目。

它工作正常但过滤后的列表包含 EMPTY 项目以及过滤后的项目。

我发现我应该在重新填充之前清除适配器,对吗?

会不会像

adapter.clear();
listview.getAdapter().notifyDataSetChanged();

无论如何我找不到可行的解决方案。

请问有任何修复此错误的线索吗?

public class CustomListAdapter extends ArrayAdapter<UnitView> {

    private static final String TAG = "FRAGMENT TWO";
    private final Activity context;
    private final UnitView[] dataSource;
    private UnitView[] unitViews;

    public CustomListAdapter(Activity context, UnitView[] units) {
        super(context, R.layout.unit, units);

        this.context = context;
        this.unitViews = units;
        this.dataSource = units;
    }

    public View getView(int position,View view,ViewGroup parent) {
        view = null;
        try
        {

         if (view == null)
         {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.listview_item, null);

            UnitView uv = unitViews[position];
            view.setTag(uv);

             TextView textViewTypeName = (TextView) view.findViewById(R.id.textViewTypeName);
             textViewTypeName.setText(uv.getDeviceTypeName());

             // Code to populate widgets ....

        }

        }
        catch (Exception ex)
        {

            Log.d(TAG, ex.getMessage());
        }

        return view;
    };

    // Filter input data in ListFragment
    public void filter(String charText) {

        try
        {
            charText = charText.toLowerCase(Locale.getDefault());

            unitViews = new UnitView[dataSource.length];

            if (charText.length() == 0) {
                unitViews = Arrays.copyOf(this.dataSource, this.dataSource.length);
            }
            else
            {
                List<UnitView> result = new ArrayList<UnitView>();
                for (UnitView uv : dataSource)
                {
                    if (uv.getNumber().toLowerCase(Locale.getDefault()).contains(charText))
                    {
                        result.add(uv);
                    }
                }

                if(result.size() > 0)
                {
                    unitViews = result.toArray(new UnitView[result.size()]);
                }
            }

            notifyDataSetChanged();
        }
        catch (Exception ex)
        {
            Log.d(TAG, ex.getMessage());
        }
    }

    public static boolean isBlank(String value) {
        return (value == null || value.equals("") || value.equals("null") || value.trim().equals(""));
    }
}

FragmentTwo.java

protected void onPostExecute(String s) {
            super.onPostExecute(s);

            try {
                Gson gson = new Gson();
                unitViews = gson.fromJson(s, UnitView[].class);

                if (unitViews.length > 0) {

                    ArrayList<String> names = new ArrayList<String>();

                    for (int i = 0; i < unitViews.length; i++) {

                        String name = unitViews[i].getName();
                        names.add(name);
                    }
                    String[] values = names.toArray(new String[unitViews.length]);

                    adapter = new CustomListAdapter(getActivity(), unitViews);
                    setListAdapter(adapter);

                    // Locate the EditText in listview_main.xml
                    editSearch = (EditText)getActivity().findViewById(R.id.editSearch);

                    // Capture Text in EditText
                    editSearch.addTextChangedListener(new TextWatcher() {

                        @Override
                        public void afterTextChanged(Editable arg0) {                         

                            // TODO Auto-generated method stub
                            String text = editSearch.getText().toString().toLowerCase(Locale.getDefault());
                            adapter.filter(text);
                        }

                        @Override
                        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                        }

                        @Override
                        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                        }
                    });

                    Log.d(TAG, s);
                }
            }
            catch (Exception ex)
            {
                Log.e(TAG, "Error: ", ex);
            }
        }

好的,我通过将过滤器代码重新定位到 onPostExecute 并重新初始化适配器找到了解决方案。

protected void onPostExecute(String s) {
            super.onPostExecute(s);

            try {
                Gson gson = new Gson();
                unitViews = gson.fromJson(s, UnitView[].class);

                if (unitViews.length > 0) {

                    ArrayList<String> names = new ArrayList<String>();

                    for (int i = 0; i < unitViews.length; i++) {

                        String name = unitViews[i].getName();
                        names.add(name);
                    }
                    String[] values = names.toArray(new String[unitViews.length]);

                    adapter = new CustomListAdapter(getActivity(), unitViews);
                    setListAdapter(adapter);


                    // Locate the EditText in listview_main.xml
                    editSearch = (EditText)getActivity().findViewById(R.id.editSearch);

                    // Capture Text in EditText
                    editSearch.addTextChangedListener(new TextWatcher() {

                        @Override
                        public void afterTextChanged(Editable arg0) {

                            // TODO Auto-generated method stub
                            String text = editSearch.getText().toString().toLowerCase(Locale.getDefault());


                            // adapter.filter(text);

                            try
                            {
                                text = text.toLowerCase(Locale.getDefault());



                                UnitView[] resultUnitViews = new UnitView[0];

                                if (text.length() == 0) {
                                    resultUnitViews = Arrays.copyOf(unitViews, unitViews.length);
                                }
                                else
                                {
                                    List<UnitView> result = new ArrayList<UnitView>();
                                    for (UnitView uv : unitViews)
                                    {
                                        if (uv.getNumber().toLowerCase(Locale.getDefault()).contains(text))
                                        {
                                            result.add(uv);
                                        }
                                    }

                                    if(result.size() > 0)
                                    {
                                        resultUnitViews = result.toArray(new UnitView[result.size()]);

                                        adapter = new CustomListAdapter(getActivity(), resultUnitViews);
                                        setListAdapter(adapter);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.d(TAG, ex.getMessage());
                            }
                        }

                        @Override
                        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                        }

                        @Override
                        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                        }
                    });

                    Log.d(TAG, s);
                }
            }
            catch (Exception ex)
            {
                Log.e(TAG, "Error: ", ex);
            }
        }