How to refer to the starting position of a list item when a text filter is enabled?

When I use the edit text to filter items, the items in the list are all messed up and the items will no longer give the correct intention. Any help is appreciated

lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String mName = filteredValues.get(position).getName().toString(); String mWeb = filteredValues.get(position).getWebsite().toString(); Intent openDetails = new Intent(Test.this, ResourceDetails.class); Bundle b = new Bundle(); b.putString("name", mName); b.putString("web", mWeb); openDetails.putExtras(b); startActivity(openDetails); } }); private TextWatcher filterTextWatcher = new TextWatcher(){ public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { adapter.getFilter().filter(s); adapter.notifyDataSetChanged(); } public void afterTextChanged(Editable s) { } }; 
+2
source share
4 answers
  flashsearchList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Integer temp=flashSearchNameMap.get(adapter.getItem(position)); navigateSearch(temp); } }); 

(adapter.getItem (position) will return the exact name of the list to you, and in flashSearchNameMap I saved the names and positions from oncreate to filtering. In this way, you can get the exact position by

+2
source

Position position is not reliable when using lists. I recommend that you use view.setTag(Object) to assign an identifier to each element when attaching content. It can be a number, a string, or whatever. Then you can simply access it using view.getTag() inside the click listener.

+1
source

Assuming you use your own bean to store your website names and values ​​and ArrayAdapter to show them in your ListView , e.g.

 public class NamedLink { final String mName; final String mWebsite; public NamedLink(String name, String website) { mName = name; mWebsite = website; } @Override public String toString() { return mName; } } 

With the adapter, define something like this:

 mAdapter = new ArrayAdapter<NamedLink>(this, android.R.layout.simple_list_item_2, mLinks) { @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(WhateverYourActivityIsNamed.this).inflate(android.R.layout.simple_list_item_2, null); } NamedLink link = getItem(position); // This probably deserves a ViewHolder ((TextView) convertView.findViewById(android.R.id.text1)).setText(link.getName()); ((TextView) convertView.findViewById(android.R.id.text2)).setText(link.getWebsite()); return convertView; } }; 

When filtering the array adapter, it will match beans #toString() , which in this case returns the name. When filtering, the array adapter maintains a properly indexed copy of your beans list internally - that is, you can use the position that you get in the click listener as follows:

 getListView().setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // getItemAtPosition() will return a NamedLink from the filtered // list maintained inside the ArrayAdapter NamedLink link = (NamedLink) parent.getItemAtPosition(position); Intent openDetails = new Intent(Test.this, ResourceDetails.class); Bundle b = new Bundle(); b.putString("name", link.getName()); b.putString("web", link.getWebsite()); openDetails.putExtras(b); startActivity(openDetails); } }); 
+1
source

if you use ViewHolder in the Adapter , just define the realPosition variable in the holder class and set it to YourAdapter.getView

and in ListClick Listener

  ContactAdapter.ViewHolder holder = (YourAdapter.ViewHolder) view.getTag(); holder.realPosition 
+1
source

Source: https://habr.com/ru/post/957899/


All Articles