Friday 15 April 2011

android - Custom filtering for Custom ArrayAdapter in ListView -


I type my own array adapter like this:

  public class PoiListAdapter ArrayAdapter and Lt; Poi & gt; Filterable {Private context reference; Personal Final List & lt; Poi & gt; ValuesPoi; Private items filter amfilter; Public Polymer Adapter (reference reference, list & quot; Poi & gt; values ​​poi) {super (references, R. lyte poolist); This.context = context; This.valuesPoi = valuesPoi; } @ Override Public View getView (see integer status, seeviewview, ViewGroup parent) {layoutInfleter Inflator = (layout inferator) reference .getSystemService (Reference. LAOUT_INFLATER_SERVICE); See line view = inflater.inflate (R.layout.poilist, guardian, wrong); TextView Text ViewName = (TextView) rowView.findViewById (R.id.name_poi); TextView Text ViewDis = (TextView) Queue FindViewById (R.id.discrip_poi); TextViewName.setText (valuesPoi.get (status) .getName ()); TextViewDis.setText (valuesPoi.get (status) .getDiscription ()); Return line view; } / ** * Apply filterable interface * / public filter getFilter () {if (mFilter == faucet) {mFilter = New item filter (this); } Return mFilter; } Public listing & lt; Poi & gt; GetValuesPoi () {return values ​​Poi; } Public Zero addValuesPoi (Poi P) {valuesPoi.add (P); } @ Override Public Zero Clear () {valuesPoi.clear (); }}   

I want to apply a search function for this adapter. So I apply a custom filter-class:

  increases the public class item filter filter {Private poolist adapter poiListAdapter; Public item filter (polyst Adapter Political Adapter) {this.poiListAdapter = poiListAdapter; } @ Override protected filter responses filtering (blocking the squares) {constraint = constraintto string (). ToLowerCase (); Filter result result = new filter result (); ArrayList & LT; Poi & gt; FilterList = new arreelist & lt; Poi & gt; (); If (obstacle! = Null & barrier .to string (). Length ()> gt;) {ArrayList & lt; Poi & gt; OrginalList = new array list & lt; Poi & gt; (PoiListAdapter.getValuesPoi ()); (POP: Organic List) {if (PJnetN). ToLowerCase (). (Handicap in it) filterList.add (p); } Log.i ("DEBUG", orginalList.toString ()); Result.values ​​= filterList; Results count = filterLize.size (); } And {result.values ​​= poiListAdapter.getValuesPoi (); Result.count = poiListAdapter.getValuesPoi (). Shape (); } Return results; } @SuppressWarnings ("uncheck") @ Override protected zero publishing (squared barrier, filter result result) {ArrayList & lt; Poi & gt; Fittoms = (arrelisted & lt; Poi & gt;) results.values; PoiListAdapter.clear (); For (Poi P: Fitkam) {poiListAdapter.addValuesPoi (P); PoiListAdapter.notifyDataSetChanged (); }}   

1 problem

.... this is a java.util.concurrentmodificationforference:

  for (PoP: Fitkam) {poiListAdapter.addValuesPoi (P); PoiListAdapter.notifyDataSetChanged (); }   

I think the problem is that I want to modify an Arrayi under Access. I think I have to work with synchronized, but I've never worked with it before.

Update: This issue is resolved! Here the code:

 for  (Iterator 
  • Poetry; i = fitems.iterator (); i.hasNext ();) {Poo P = i.next (); PoiListAdapter.addValuesPoi (P); //poiListAdapter.notifyDataSetChanged (); }

    2. Problem

    List view is empty when you start. Initially I want to show all the elements! Apart from this, nothing is displayed by an element search! The list view shows nothing!

    Two types of processed improvements can be avoided:

    1. poiListAdapter.addValuesPoi (p) Add this code to a synchronized method. The synchronized method can not be accessed simultaneously.

    2. Use the archive. Synchronized

  • No comments:

    Post a Comment