You need to implement the correct transition to the new activit, using a ListView, SearchView, ListVIewAdapter. If you do not use the search, then the position works correctly, but if you find some element in the search, for example, its position = 9, and the search will be 0. How can I fix it?

AnimalNames for ListView

public class AnimalNames { private String animalName; private String country; private Integer population; private Integer population_a; public AnimalNames(String country, String animalName, Integer population, Integer population_a) { this.animalName = animalName; this.country = country; this.population = population; this.population_a = population_a; } public String getAnimalName() { return this.animalName; } public String getCountry() { return this.country; } public Integer getPopulation() { return this.population; } public Integer getPopulation_a() { return this.population_a; } } 

for listview

 public class ListViewAdapter extends BaseAdapter{ Context mContext; LayoutInflater inflater; private List<AnimalNames> animalNamesList = null; private ArrayList<AnimalNames> arraylist; public ListViewAdapter(Context context, List<AnimalNames> animalNamesList) { mContext = context; this.animalNamesList = animalNamesList; inflater = LayoutInflater.from(mContext); this.arraylist = new ArrayList<AnimalNames>(); this.arraylist.addAll(animalNamesList); } public class ViewHolder { TextView name; TextView tema; ImageView imgid; ImageView imgid1; Button button; } @Override public int getCount() { return animalNamesList.size(); } @Override public AnimalNames getItem(int position) { return animalNamesList.get(position); } @Override public long getItemId(int position) { return position; } public View getView(final int position, View view, ViewGroup parent) { final ViewHolder holder; if (view == null) { holder = new ViewHolder(); view = inflater.inflate(R.layout.content_main, null,true); // Locate the TextViews in listview_item.xml holder.name = (TextView) view.findViewById(R.id.name_tema); holder.tema = (TextView) view.findViewById(R.id.txtStatusMsg); holder.imgid = (ImageView) view.findViewById(R.id.profilePic); holder.imgid1 = (ImageView) view.findViewById(R.id.profile); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } // Set the results into TextViews holder.name.setText(animalNamesList.get(position).getAnimalName()); holder.tema.setText(animalNamesList.get(position).getCountry()); holder.imgid.setImageResource(animalNamesList.get(position).getPopulation()); holder.imgid1.setImageResource(animalNamesList.get(position).getPopulation_a()); Button button = (Button) view.findViewById(R.id.butt); button.setClickable(false); button.setFocusable(false); return view; } // Filter Class public void filter(String charText) { charText = charText.toLowerCase(Locale.getDefault()); animalNamesList.clear(); if (charText.length() == 0) { animalNamesList.addAll(arraylist); } else { for (AnimalNames wp : arraylist) { if (wp.getAnimalName().toLowerCase(Locale.getDefault()).contains(charText) ||wp.getCountry().toLowerCase(Locale.getDefault()).contains(charText) ) { animalNamesList.add(wp); } } } notifyDataSetChanged(); } } 

MainActivity

  list = (ListView) findViewById(R.id.list); for (int i = 0; i < animalNameList.length; i++) { AnimalNames animalNames = new AnimalNames(animalNameList[i],country[i],population[i],population_a[i]); // Binds all strings into an array arraylist.add(animalNames); } // Pass results to ListViewAdapter Class adapter = new ListViewAdapter(this, arraylist); // Binds the Adapter to the ListView list.setAdapter(adapter); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, final long id) { } }); // Locate the EditText in listview_main.xml editsearch = (SearchView) findViewById(R.id.search_view); editsearch.setOnQueryTextListener(this); @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { String text = newText; adapter.filter(text); return false; } 
  • This happens because after the search you create a new list with your own positions. In the model, start another position field and read the position from it, not the position in the list. - pavlofff
  • @pavlofff, and an example is possible, because I just started to study, I would be very grateful to you - fcbarcafc

1 answer 1

In the model class, add the position field (set through the first argument of the constructor):

 public class AnimalNames { ... private long position; public AnimalNames(long position, String country, String animalName, Integer population, Integer population_a) { ... this.position = position; } ... public long getPosition() { return this.position; } } 

In the adapter, we make a method that returns an absolute position. We will use the fourth argument of the onItemClick onItemClick() id , since it is still not used in lists not related to the database and this will save us from unnecessary code:

 public class ListViewAdapter extends BaseAdapter{ ... public ListViewAdapter(Context context, List<AnimalNames> animalNamesList) { ... } @Override public long getItemId(int position) { return animalNamesList.get(position).getPosition(); } public View getView(final int position, View view, ViewGroup parent) { ... } } 

Now we need to fill in the absolute positions in the model (we specify the loop counter as the position in the first argument):

 list = (ListView) findViewById(R.id.list); for (int i = 0; i < animalNameList.length; i++) { AnimalNames animalNames = new AnimalNames(i, animalNameList[i],country[i],population[i],population_a[i]); arraylist.add(animalNames); } adapter = new ListViewAdapter(this, arraylist); list.setAdapter(adapter); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // здесь при клике на айтеме возвращается третий аргумент // текущая позиция в списке (position) - с учетом фильтра. // Четвертый аргумент (id) - абсолютная позиция, без учета фильтра } }); 

Unchanged parts of the code are omitted.

  • Thanks for the detailed explanation, and you can write to me for an example what exactly you need to write in onItemClick, because it does not work out for me, thanks in advance. - fcbarcafc
  • @fcbarcafc Where you need the value of the position that was in the full list, write the id variable - pavlofff
  • Yes, I understood everything. Thank you very much for your help - fcbarcafc