我有一个自动完成文本,我可以使用volley stringrequest从Web服务获取建议。现在的问题是ArrayList没有被清除,因此每次我发出请求时,旧项目都会被添加到列表中。我试过adapter.clear()
但它给了我NullPointerException。所以任何帮助清除这一点,以便列表不包含旧项目将非常感激。
LogCat:
FATAL EXCEPTION: main
Process: com.sam.ubooktoday, PID: 24983
java.lang.NullPointerException
at com.sam.ubooktoday.view.fragments.HomeOptionTwo$1.onResponse(HomeOptionTwo.java:88)
at com.sam.ubooktoday.view.fragments.HomeOptionTwo$1.onResponse(HomeOptionTwo.java:69)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5388)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:655)
at dalvik.system.NativeStart.main(Native Method)

// if you press 'na' json response is:
[
"Manicure and Pedicure ",
"Manicures",
"Pedicures",
"Student Specials Under 18 Years",
"Hand & Foot Care and Repair",
"Nail Art Design ",
"Polish Change",
"Kids Menu under 12 years",
"Healthy Nail Dip in powder",
"Nail Enhancement",
"Full Set",
"Fill Ins"
]

public class HomeOptionTwo extends Fragment implements TextWatcher{
AutoCompleteTextView autoservice;
ArrayList<String> arrList = new ArrayList<String>();
ArrayAdapter<String> adapter;
private static final String SERVICE = "http://example.com/android/services";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.homeoptiontwo, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
autoservice = (AutoCompleteTextView)view.findViewById(R.id.service);
autoservice.addTextChangedListener(this);
}
private void prepareMyList() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, SERVICE,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonarray = new JSONArray(response);
for (int i = 0; i < jsonarray.length(); i++) {
//Toast.makeText(getActivity(), "services:" + jsonarray.optString(i), Toast.LENGTH_SHORT).show();
String strValue = jsonarray.getString(i);
arrList.add(strValue);
}
adapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_dropdown_item_1line,
arrList);
autoservice.setThreshold(1);
if(adapter!=null){adapter.clear();}
autoservice.setAdapter(adapter);
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getActivity(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("phrase", autoservice.getText().toString());
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
prepareMyList();
}
@Override
public void afterTextChanged(Editable s) {
}
}
&#13;
答案 0 :(得分:1)
您不需要清除适配器,因为您每次都要设置新适配器,但需要清除列表,以便从列表中获取和显示新数据。
所以在prepareMyList()方法中,在try catch方法中首先写下JSONArray之前的行jsonarray = new JSONArray(response);线。
所以它看起来像,
arrList = new ArrayList<String>();
JSONArray jsonarray = new JSONArray(response);
然后检查它是否会解决您的问题。
答案 1 :(得分:1)
最近对我说了同样的事情,我的问题是过滤器正在过滤我的结果,因为我的结果没有与键入的文本字符串匹配。也可能是你的情况。
在您的阵列适配器上尝试@Override
getFilter()
并返回无效的new Filter()
。喜欢:
public Filter getFilter() {
return new Filter() {
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
return null;
}
};
}