0
我的资产文件夹中有Json文件。我正在使用AutoCompleteTextView来过滤使用php作为Json的远程服务器的结果。但我想用我的Json文件而不是服务器响应Json。使用Json文件而不是服务器响应并将其用于过滤
这是我的代码。
private static final String url = AppConfig.URL_JSON_AVAILABLE_PRODUCTS;
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if ((constraint == null || constraint.length() == 0)) {
filterResults.values = locations;
filterResults.count = locations.size();
} else {
mResults = getParseJsonWCF(constraint.toString());
locations.clear();
for (int i = 0; i < mResults.size(); i++) {
if (mResults.get(i).getMatName().toUpperCase().startsWith(constraint.toString().toUpperCase())) {
//a = new_suggestions.get(i).getMatNo();
locations.add(mResults.get(i));
}
}
// Assign the data to the FilterResults
filterResults.values = locations;
filterResults.count = locations.size();
//notifyDataSetChanged();
}
return filterResults;
}
@SuppressWarnings("unchecked cast")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
mResults = (List<Product>) results.values;
//setArrayList(mResults);
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
}
public List<Product> getParseJsonWCF(String sName) {
List<Product> ListData = new ArrayList<Product>();
try {
String temp = sName.replace(" ", "%20");
URL js = new URL(url + temp);
URLConnection jc = js.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
String line = reader.readLine();
JSONObject jsonResponse = new JSONObject(line);
JSONArray jsonArray = jsonResponse.getJSONArray("feed");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject r = jsonArray.getJSONObject(i);
ListData.add(new Product(r.getInt("matnum"), r.getString("matname"), r.getString("code"), r.getString("matgrp"), r.getString("mattype")));
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return ListData;
}
我想用我的Json文件的数据来代替这里的url。我不知道如何实现这一点,把它的URL