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; }
positionfield and read the position from it, not the position in the list. - pavlofff