0

I am trying to create search bar that searches through json data from a server and presents the results in a ListView. The data is in the form of an array. For Example

ProductList: [

{ ProductCode: "10012010",
  ProductName: "Kell", 
  ProductGrammage: "120 gm", 
  ProductBarcode: "890123456789", 
  ProductCatCode: "40", 
  ProductCatName: "Packed Food and Condiments",
  ProductSubCode: "4001", 
  ProductSubCodeName: "Breakfast & Cereals",
  ProductMRP: "120", 
  ProductBBPrice: "115" },

ect...

]

So lets say I type in Kell in the search bar. I want this Kell object to pop up in my list view.

Jamie Eltringham
  • 810
  • 3
  • 16
  • 25
Evan Dix
  • 17
  • 5

1 Answers1

1

Parse the JSON and put the result into an ArrayList. You need an ArrayAdapter that implements the Filterable interface and set it to the ListView.

Your ArrayAdapter should look similar to:

private class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
    private ArrayList<String> resultList;

    public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

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

    @Override
    public String getItem(int index) {
        return resultList.get(index);
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    // Retrieve the autocomplete results.
                    resultList = autocomplete(constraint.toString());

                    // Assign the data to the FilterResults
                    filterResults.values = resultList;
                    filterResults.count = resultList.size();
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                }
                else {
                    notifyDataSetInvalidated();
                }
            }};
        return filter;
    }
}

Code from here.

LordRaydenMK
  • 13,074
  • 5
  • 50
  • 56