如何使用 json 文件显示自动完成字段的建议?

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

我有这个 json,在这个产品代码中有“产品代码”,还有另一个 json 对象和字符串。

我想要的是当我输入 "product code" 时会显示建议的描述。

我输入产品描述代码时已经显示了建议描述,但似乎显示方式不正确。

我的意思是,当我输入 "product code" "BI" 时,建议描述的显示是这样的

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

这是我的 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.. { 
                    }
}

这是我的代码,用于将我的 json 解析为我的片段

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();
        }
    }

下面是我得到的品牌动态字段

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

我希望输出应该是这样的

输入文本:T

建议 DISPLAY:GIANT 崔克 圣诞老人 斯科特

显示"T"

的所有值

实际产量

输入文本:BI

建议显示:{"GNT":"GIANT","TRL":"TREEK","CNN":"CNDALE","ST" : "SANTA","SCT": "SCOTT"}

  1. 在这行代码中 - mArrayStringLookupProduct.add(String.valueOf(brand)); 您正在将整个 jsonObject 添加到 mArrayStringLookupProduct ArrayList 中。
  2. 您只需要此 ArrayList 中的 JSONObject 品牌值。

您可以进行以下更改-

   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));
   }