AutoCompleteTextView with volley with text filter
AutoCompleteTextView with volley with text filter
我正在尝试从 sql table 中过滤我的数据。这在我的查询中。
SELECT TOP 10 ItemCode, ItemName
FROM [mytable]
WHERE ItemCode like '".$q."%' order by CreatedDate desc
这是我的截击
private void getItem(String s){
JsonArrayRequest postRequest = new JsonArrayRequest (Request.Method.GET, "http://192.168.3.9/api/welcome/getItem/"+s,null,
new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response) {
try {
if (itemList == null) itemList = new ArrayList<>();
itemList.clear();
for(int i = 0; i < response.length(); i++){
Item_model m_item = new Item_model();
List<String> list = new ArrayList<String>();
m_item.setItemCode(response.getJSONObject(i).getString("ItemCode"));
itemList.add(m_item);
}
itemcode.setAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("ErrorResponse", error.toString());
}
}
);
postRequest.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
postRequest.setTag("GetItem");
queue.add(postRequest);
}
我调用里面的getItem onCreateView
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getItem("");
}
现在我的代码工作正常,因为我想过滤所以我使用 TextWatcher
,这就是我所做的。
itemcode.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
search = s.toString();
getItem("");
adapter.getFilter().filter(search);
}
});
当我执行上面的脚本时,我的自动完成文本视图没有显示任何数据,我该如何解决?
我的json
[{"ItemCode":"IT001","ItemName":"Blue Hat"},{"ItemCode":"IT002","ItemName":"Black Hat"}]
我已经用@pskink 的方式修复了它。我不使用自定义适配器。
我的自动完成的最终代码是这样的
itemcode = v.findViewById(R.id.ItemCodeInput);
itemcode.setThreshold(1);
String[] from = { "name", "description" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleCursorAdapter a = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2, null, from, to, 0);
a.setStringConversionColumn(1);
FilterQueryProvider provider = new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
if (constraint == null) {
return null;
}
String[] columnNames = { BaseColumns._ID, "name", "description" };
MatrixCursor c = new MatrixCursor(columnNames);
try {
String urlString = "http://192.168.3.223:84/lesfemmesapi/welcome/getItem/"+constraint;
URL url = new URL(urlString);
InputStream stream = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String jsonStr = reader.readLine();
JSONObject result = new JSONObject(jsonStr);
JSONArray jArray = result.getJSONArray("dataItem");
for(int i = 0; i < jArray.length();i++) {
JSONObject detail = jArray.getJSONObject(i);
c.newRow().add(i).add(detail.getString("ItemCode")).add(detail.getString("ItemName"));
}
} catch (Exception e) {
e.printStackTrace();
}
return c;
}
};
a.setFilterQueryProvider(provider);
我正在尝试从 sql table 中过滤我的数据。这在我的查询中。
SELECT TOP 10 ItemCode, ItemName
FROM [mytable]
WHERE ItemCode like '".$q."%' order by CreatedDate desc
这是我的截击
private void getItem(String s){
JsonArrayRequest postRequest = new JsonArrayRequest (Request.Method.GET, "http://192.168.3.9/api/welcome/getItem/"+s,null,
new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response) {
try {
if (itemList == null) itemList = new ArrayList<>();
itemList.clear();
for(int i = 0; i < response.length(); i++){
Item_model m_item = new Item_model();
List<String> list = new ArrayList<String>();
m_item.setItemCode(response.getJSONObject(i).getString("ItemCode"));
itemList.add(m_item);
}
itemcode.setAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("ErrorResponse", error.toString());
}
}
);
postRequest.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
postRequest.setTag("GetItem");
queue.add(postRequest);
}
我调用里面的getItem onCreateView
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getItem("");
}
现在我的代码工作正常,因为我想过滤所以我使用 TextWatcher
,这就是我所做的。
itemcode.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
search = s.toString();
getItem("");
adapter.getFilter().filter(search);
}
});
当我执行上面的脚本时,我的自动完成文本视图没有显示任何数据,我该如何解决?
我的json
[{"ItemCode":"IT001","ItemName":"Blue Hat"},{"ItemCode":"IT002","ItemName":"Black Hat"}]
我已经用@pskink 的方式修复了它。我不使用自定义适配器。
我的自动完成的最终代码是这样的
itemcode = v.findViewById(R.id.ItemCodeInput);
itemcode.setThreshold(1);
String[] from = { "name", "description" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleCursorAdapter a = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2, null, from, to, 0);
a.setStringConversionColumn(1);
FilterQueryProvider provider = new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
if (constraint == null) {
return null;
}
String[] columnNames = { BaseColumns._ID, "name", "description" };
MatrixCursor c = new MatrixCursor(columnNames);
try {
String urlString = "http://192.168.3.223:84/lesfemmesapi/welcome/getItem/"+constraint;
URL url = new URL(urlString);
InputStream stream = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String jsonStr = reader.readLine();
JSONObject result = new JSONObject(jsonStr);
JSONArray jArray = result.getJSONArray("dataItem");
for(int i = 0; i < jArray.length();i++) {
JSONObject detail = jArray.getJSONObject(i);
c.newRow().add(i).add(detail.getString("ItemCode")).add(detail.getString("ItemName"));
}
} catch (Exception e) {
e.printStackTrace();
}
return c;
}
};
a.setFilterQueryProvider(provider);