Timothy Frisch
Timothy Frisch

Reputation: 2809

Filtering listview of custom POJO's by their fields

I have a POJO:

public class RoomData implements Parcelable {
    private String objectId, name, building, floor, imageUrl;
    private boolean hasPc, hasProjector, hasTelepresence, hasWhiteboard;
    private boolean dataReady = false;
    private ArrayList<MeetingData> meetingData = new ArrayList<MeetingData>();
    private int capacity;
    private ArrayList<BeaconData> beacons = new ArrayList<BeaconData>();
    private DateTime nextAvailableStart, nextAvailableEnd;
    private boolean availableNow = false;
    private ArrayList<Interval> meetingDuringIntervals = new ArrayList<Interval>();
}

that is populated into a custom ListView/ListAdapter.

I want to know how I can filter that list by the fields of my POJO. For instance if I construct a RoomFilter object, that states that my capacity integer should be higher than X. How can I implement this? I can only find links to filtering by primitive data types...

Here is my custom Adapter class:

public class BeaconAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<BLEDeviceAdvertisement> beaconArrayList = new ArrayList<BLEDeviceAdvertisement>();

    private static LayoutInflater inflater = null;

    //BeaconAdapter for the custom ListView
    public BeaconAdapter(Context context, ArrayList<BLEDeviceAdvertisement> beaconArrayList) {
        this.beaconArrayList = beaconArrayList;
        this.context = context;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return beaconArrayList.size();
    }

    @Override
    public BLEDeviceAdvertisement getItem(int position) {
        return beaconArrayList.get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    //Custom getview for the custom layout beacon_row_item.
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View myCustomView = convertView;

        if (myCustomView == null)
            myCustomView = inflater.inflate(R.layout.beacon_row_item, null);

        return myCustomView;
    }

}

Upvotes: 1

Views: 404

Answers (1)

Gil Vegliach
Gil Vegliach

Reputation: 3562

After OP's request I post my comment as an answer for future reference, giving more details. Here are some possible solutions:

  1. Destructive update: Loop through the list, filtering out what you don't need. Implement adapter based on this list.

  2. Virtual view: Implement logic for a "virtual view" over the original list. Here you keep only the original list, but in all adapter's methods you skip items that don't match the filter. For instance, in getItem(int pos) you loop through the list keeping a counter, but you increment it only if the item matches the filter: when the counter equals pos you return the item. Similar for the other methods.

  3. Concrete View: Use an additional list to keep the "view" over the original list, so to cache the computation done in 2. When the filter is set/updated, a loop through the original list builds the "view" and then you implement the adapter based on the "view". More time efficient, but also more memory consumed.

Upvotes: 1

Related Questions