GUJ Discussões   :   últimos tópicos   |   categorias   |   GUJ Respostas

SearchView retorna valor certo com click errado

java
programação
android
Tags: #<Tag:0x00007f18648c4870> #<Tag:0x00007f18648c4500> #<Tag:0x00007f18648c4190>

#1

Boa tarde, Eu implementei um search no meu código e ele esta funcionando muito bem no quesito de trazer o que estou procurando, porem quando clico ele abre a pagina do primeiro valor do meu array, exemplo:
pagina principal
bola click ----- 2 pagina bola
carro click ------ 2 pagina carro
quando dou um search
search ----- carro click 2 pagina -----bola
nao consigo entender o porque disso, alguém poderia me ajudar?

segue código

public class MainActivity extends AppCompatActivity {

ArrayList<String> serieNameList;
ListView serieNameLV;
SearchView serieNameSearchSV;
CustomAdapter adapter;

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

    
    serieNameLV = findViewById(R.id.recyclerviewid);
    serieNameSearchSV = findViewById(R.id.searchSerie);

    Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.BASE_URL).
            addConverterFactory(GsonConverterFactory.create()).build();

    Api api = retrofit.create(Api.class);

    Call<ArrayList<Model>> call = api.getModelList();

    call.enqueue(new Callback<ArrayList<Model>>() {
        @Override
        public void onResponse(Call<ArrayList<Model>> call, Response<ArrayList<Model>> response) {
            serieNameList = new ArrayList<String>();
            ArrayList<Model> serieList = response.body();

            for (Model model:serieList){

                serieNameList.add(model.getImage_url());
                serieNameList.add(model.getName());
                serieNameList.add(model.getAno());
                serieNameList.add(model.getCategorie());
                serieNameList.add(model.getDescription());


            }

            adapter = new CustomAdapter(getApplicationContext(), R.layout.serie_row_item, serieList);
            serieNameLV.setAdapter(adapter);

        }

        @Override
        public void onFailure(Call<ArrayList<Model>> call, Throwable t) {

        }
    });

    serieNameSearchSV.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            adapter.getFilter().filter(newText);

            
            return false;
        }
    });
    
    

}

class CustomAdapter extends ArrayAdapter<Model> {

    ArrayList<Model> series;
    Context context;
    int resource;

    public CustomAdapter(Context context, int resource, ArrayList<Model> series) {
        super(context, resource, series);
        this.series = series;
        this.context = context;
        this.resource = resource;
    }
    


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

        if(convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater)getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.serie_row_item, null, true);
        }

        Model model = getItem(position);

        ImageView imageView = (ImageView)convertView.findViewById(R.id.thumbnail);
        Glide.with(context).load(series.get(position).getImage_url()).into(imageView);

        TextView serie_name = (TextView)convertView.findViewById(R.id.serie_name);
        TextView serie_ano = (TextView)convertView.findViewById(R.id.ano_serie);
        TextView serie_categoria = (TextView)convertView.findViewById(R.id.categorie);


        serie_name.setText(model.getName());
        serie_ano.setText(model.getAno());
        serie_categoria.setText(model.getCategorie());

        serieNameLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent i = new Intent(MainActivity.this, SerieActivity.class);
                i.putExtra("serie_name",series.get(position).getName());
                i.putExtra("serie_description",series.get(position).getDescription());
                i.putExtra("serie_category", series.get(position).getCategorie());
                i.putExtra("serie_ano", series.get(position).getAno());
                i.putExtra("serie_img", series.get(position).getAno());
                startActivity(i);


            }
        });



        return convertView;
    }
}

}