Android ArrayAdapter无法过​​滤,因为未设置mOriginalValues

时间:2019-02-02 12:58:28

标签: android

我的问题是我无法使用自定义的过滤器过滤ListView。过滤器会打​​印出所需的结果,但视图不会更新。

这是我的活动:

public class RecipesActivity extends AppCompatActivity implements RecipeListFragment.OnListFragmentInteractionListener {

private TextView mTextMessage;
private ListView listView;
private RecipeListAdapter sIAdapter;
private EditText searchRecipe;
private ArrayList<Recipe> recipes;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipes);
    searchRecipe = (EditText) findViewById(R.id.searchRecipe);
    mTextMessage = (TextView) findViewById(R.id.message);

     recipes =SQLGetter.readRecipesFromDB(this,"");


    listView = (ListView) findViewById(R.id.recipeList);
    sIAdapter = new RecipeListAdapter(this,R.layout.recipe_list_row, recipes);
    listView.setAdapter(sIAdapter);

    searchRecipe.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            sIAdapter.getFilter().filter(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

}

这是我的适配器:

public class RecipeListAdapter extends ArrayAdapter<Recipe> implements Filterable {

private ArrayList<Recipe> arr;
private ArrayList<Recipe> filteredRecipes;
private TextView text;


public RecipeListAdapter(Context context, int textviewResourceId, ArrayList<Recipe> recipes){
    super(context,textviewResourceId,recipes);
    this.arr =recipes;
    this.filteredRecipes=new ArrayList<Recipe>();

    }

    @Override
    public View getView (final int position, View convertView, ViewGroup parent){

            final Recipe recipe = getItem(position);
            if(convertView==null) {
                convertView= LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_list_row, parent, false);
            }
            final TextView recipeName = convertView.findViewById(R.id.recipeName);
            recipeName.setText(recipe.getName());

        if(!recipeName.getText().toString().isEmpty()) {
            recipeName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(), RecipeShowcaseActivity.class);
                    intent.putExtra("recipeName", recipe.getName());
                    getContext().startActivity(intent);
                }
            });
        }
        System.out.println("Arraygröße ="+ arr.size());
        return convertView;

    }

@Override
public Filter getFilter() {


    final Filter filter = new Filter() {

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            arr = (ArrayList<Recipe>) results.values;
            System.out.println("Arrgröße : "+arr.size());
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();

            constraint = constraint.toString().toLowerCase();

            ArrayList<Recipe> filteredList = new ArrayList<Recipe>();
            ArrayList<Recipe> allRecipes =SQLGetter.readRecipesFromDB(getContext(),"");
            for(Recipe recipe:allRecipes){
                if(recipe.getName().toUpperCase().contains(constraint.toString().toUpperCase())){
                    filteredList.add(recipe);
                }
            }
            System.out.println("Filterlistengröße: " +filteredList.size());
            results.count = filteredList.size();
            results.values = filteredList;
            Log.e("VALUES", results.values.toString());

            return results;
        }
    };

    return filter;
}

}

最后,这是我在RecipesActivity中调用过滤器时面临的问题:

enter image description here

到目前为止,在我读过的每篇文章中,通常都写道,由于初始化,应该设置mOriginalValues

sIAdapter = new RecipeListAdapter(this,R.layout.recipe_list_row, recipes);

我真的不知道我在这里做错什么了。

1 个答案:

答案 0 :(得分:0)

每次在列表中进行更改后,都应使用sIAdapter.notifyDataSetChanged()