0

the code right and I re-write it more than one time and compiler without error but didn't make search

public class PokedexAdapter extends RecyclerView.Adapter<PokedexAdapter.PokedexViewHolder> implements Filterable {
List<Pokemon> filtered = new ArrayList<>();
public static final String Tag = "popo";
public static class PokedexViewHolder extends RecyclerView.ViewHolder {
    public LinearLayout containerView;
    public TextView textView;

    PokedexViewHolder(View view) {
        super(view);

        containerView = view.findViewById(R.id.pokedex_row);
        textView = view.findViewById(R.id.pokedex_row_text_view);

        containerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Pokemon current = (Pokemon) containerView.getTag();
                Intent intent = new Intent(v.getContext(), PokemonActivity.class);
                intent.putExtra("url", current.getUrl());

                v.getContext().startActivity(intent);
            }
        });
    }
}

private List<Pokemon> pokemon = new ArrayList<>();
private RequestQueue requestQueue;

PokedexAdapter(Context context) {
    requestQueue = Volley.newRequestQueue(context);
    loadPokemon();
}

public void loadPokemon() {
    String url = "https://pokeapi.co/api/v2/pokemon?limit=151";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray results = response.getJSONArray("results");
                for (int i = 0; i < results.length(); i++) {
                    JSONObject result = results.getJSONObject(i);
                    String name = result.getString("name");
                    pokemon.add(new Pokemon(
                        name.substring(0, 1).toUpperCase() + name.substring(1),
                        result.getString("url")
                    ));
                }

                notifyDataSetChanged();
            } catch (JSONException e) {
                Log.e("cs50", "Json error", e);
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("cs50", "Pokemon list error", error);
        }
    });

    requestQueue.add(request);
}

@NonNull
@Override
public PokedexViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.pokedex_row, parent, false);

    return new PokedexViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull PokedexViewHolder holder, int position) {
    if (filtered.size()==0) {
        Pokemon current = pokemon.get(position);
        holder.textView.setText(current.getName());
        holder.containerView.setTag(current);
    }else {
        Pokemon current = filtered.get(position);
        holder.textView.setText(current.getName());
        holder.containerView.setTag(current);
    }
}

@Override
public int getItemCount() {
    if (filtered.size()==0) {
        return pokemon.size();
    }else{
        return filtered.size();
    }
}
@Override
public Filter getFilter() {
    return new PokemonFilter();
}

private class PokemonFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        if (constraint == null || constraint.length() == 0) {
            results.values = pokemon;
            results.count = pokemon.size();
        }else {
            List<Pokemon> filteredPokemon = new ArrayList<>();
            for (Pokemon pokemon : filteredPokemon) {
                if (pokemon.getName().toUpperCase().startsWith(constraint.toString().toUpperCase())) {
                    filteredPokemon.add(pokemon);
                }
            }
            results.values = filteredPokemon;
            results.count = filteredPokemon.size();
        }
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filtered = (List<Pokemon>) results.values;
        notifyDataSetChanged();
    }
}

}

and this code for MainAcitivty

public class MainActivity extends AppCompatActivity implements androidx.appcompat.widget.SearchView.OnQueryTextListener {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private PokedexAdapter pokoAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = findViewById(R.id.recycler_view);
    adapter = new PokedexAdapter(getApplicationContext());
    layoutManager = new LinearLayoutManager(this);
    pokoAdapter = new PokedexAdapter(this);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(layoutManager);
}

@Override
public boolean onQueryTextSubmit(String s) {
    pokoAdapter.getFilter().filter(s);
    return false;
}

@Override
public boolean onQueryTextChange(String s) {
        pokoAdapter.getFilter().filter(s);
        return false;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.setOnQueryTextListener(this);

    return true;
}

}

1
  • Have you solved this problem? I think I have the same one – Gaspar Zuker Dec 4 '20 at 21:03

You must log in to answer this question.

Browse other questions tagged .