Search code examples
javaandroidjsonandroid-fragmentsautocompletetextview

How to display the suggestion for autocomplete field using json file?


I have this json that has " product code" inside this product code has another jsonobject and string.

What I want is that when I input the "product code" the suggested description will show.

I already get the display for suggested description when I input the product description code but it seems that it displaying in wrong way.

What I mean is that when I type the "product code" "BI" the display for suggested description is like this

{"GNT": "GIANT","TRL": "TREEK","CNN": "CNDALE","ST": "SANTA","SCT": "SCOTT"}

here is my json

{
    "BI": {
        "desc": {
            "MBIK": "MOUNTAIN BIKE",
            "FBIK": "FOLDING BIKE",
            "EBIK": "E-BIKE",
            "OTHER": "OTHER"
        },
        "brand": {
            "GNT": "GIANT",
            "TRL": "TREEK",
            "CNN": "CNDALE",
            "STC": "SANTA",
            "SCT": "SCOTT"
        },
        "category": "BICYCLE & EBIKE STORE"
    },
    "CA": {
        "desc": {
            "CARA": "CAR AUDIO",
            "CARS": "CAR SPEAKER",
            "TIRE": "CAR TIRE",
            "OTHER": "OTHER"
        },
        "brand": {
            "AIC": "AICHI",
            "BRI": "BRIGDESTONE",
            "CON": "CONTINENTAL",
            "DUN": "DUNLOP",
            "FAL": "FALKEN GR"
        }
                "category": "CAR ACCESORIES"
    }, 
        and so on.. { 
                    }
}

here is the my code to parse my json to my fragment

private void loadLookupCategoryJson() {

        mLookupProducts = new ArrayList<>();
        mArrayStringLookupProduct = new ArrayList<>();

        try {
            JSONObject json = new JSONObject(loadLookupBrandJSON());
            Iterator<String> keys = json.keys();

            while(keys.hasNext()) {
                String key = keys.next();
                JSONObject obj = (JSONObject) json.get(key);

                JSONObject brand = obj.getJSONObject(Keys.CATEGORY_BRAND);
                JSONObject desc =  obj.getJSONObject(Keys.CATEGORY_DESC);
                String category = obj.getString(Keys.CATEGORY);

                LookupProducts lookupProduct = new LookupProducts(key, brand, desc, category);
                mLookupProducts.add(lookupProduct);
                mArrayStringLookupProduct.add(String.valueOf(brand));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

and here is my to get the brand dynamic field

} else if (format.equalsIgnoreCase(Keys.AUTO_COMPLETE_FIELD)) {
 addTextViews(title);
 addAutoCompleteField(desc, inputType, title, mKeys.get(i), mArrayStringLookupProduct, maxLength, minLength);
} 

I expect the output should be like this

INPUT TEXT: T

SUGGESTED DISPLAY:GIANT TREEK SANTA SCOTT

Display all values with "T"

actual output

INPUT TEXT: BI

SUGGESTED DISPLAY:{"GNT": "GIANT","TRL": "TREEK","CNN": "CNDALE","ST": "SANTA","SCT": "SCOTT"}


Solution

    1. In this line of code - mArrayStringLookupProduct.add(String.valueOf(brand)); You are adding whole jsonObject into mArrayStringLookupProduct ArrayList.
    2. You only want JSONObject brand's values in this ArrayList.

    You can make the following changes-

       JSONObject brand = obj.getJSONObject(Keys.CATEGORY_BRAND);
       JSONObject desc =  obj.getJSONObject(Keys.CATEGORY_DESC);
       String category = obj.getString(Keys.CATEGORY);
       lookupProduct = new LookupProducts(key, brand, desc, category);
       mLookupProducts.add(lookupProduct);
    
       // iterate every brand key, fetch its value and add in arrayList
       Iterator<String> brandKeys = brand.keys();
       while(brandKeys.hasNext()) {
            String bKey = brandKeys.next();
            mArrayStringLookupProduct.add((String) brand.get(bKey));
       }