我想从服务器上显示列表用户名和他们的电话号码,作为AutoCompleteTextView下面的建议。我不需要过滤器,因为我已经从服务器获得筛选结果。我在每个将调用TextWatcher的文本更改上添加了AutoComleteTextView上的API。我做了自定义适配器,但它没有显示任何东西。
ArrayList声明为全局
ArrayList<NameModel> nameList = new ArrayList<>();
AutoCompleteAdapter autoCompleteAdapter;onCreate
autoCompleteAdapter = new AutoCompleteAdapter(this,nameList);
txtNumber.setAdapter(autoCompleteAdapter);
txtNumber.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) {
String number = s.toString();
if (number.matches("^[0-9]*$") && number.length() == 10){
btnAdd.setVisibility(View.VISIBLE);
}else {
btnAdd.setVisibility(View.GONE);
GetNames(s);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});GetNames()
nameList.clear();
autoCompleteAdapter.notifyDataSetChanged();
String params = inst_id+"/?name="+text;
StringRequest stringRequest = new StringRequest(Request.Method.GET, getResources().getString(R.string.ipmain)+GET_NAME_URL+params, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i=0;i<jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("first_name")+" "+jsonObject.getString("last_name");
String role = jsonObject.getString("role");
String number = jsonObject.getString("phone_number");
NameModel nameModel = new NameModel(name,role,number);
nameList.add(nameModel);
}
autoCompleteAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}适配器--它是父的子类
private class AutoCompleteAdapter extends ArrayAdapter<NameModel> {
Context cntx;
ArrayList<NameModel> listGlobal;
AutoCompleteAdapter(@NonNull Context context, ArrayList<NameModel> list) {
super(context,R.layout.item_attendance,list);
cntx = context;
listGlobal = list;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null){
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_attendance, parent, false);
}
TextView txtName = convertView.findViewById(R.id.txtName);
TextView txtRole = convertView.findViewById(R.id.txtGender);
TextView txtPhone = convertView.findViewById(R.id.txtPhone);
txtName.setText(listGlobal.get(position).getName());
txtRole.setText(listGlobal.get(position).getRole());
txtPhone.setText(listGlobal.get(position).getNumber());
return convertView;
}
@Override
public int getCount() {
Log.d("Adpter List:",String.valueOf(listGlobal.size()));
Log.d("Main List:",String.valueOf(nameList.size()));
return listGlobal.size();
}
}在第二个字母输入时崩溃(在第一个字母输入时没有显示任何内容)
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:349)
at android.widget.AutoCompleteTextView.buildImeCompletions(AutoCompleteTextView.java:1241)
at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1201)
at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:1084)
at android.widget.AutoCompleteTextView.-wrap2(AutoCompleteTextView.java)
at android.widget.AutoCompleteTextView$PopupDataSetObserver$1.run(AutoCompleteTextView.java:1409)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)发布于 2019-04-08 10:26:28
我认为你必须修改以下内容
1.在执行远程StringRequest之后,请确保使用新的nameList更新AutoCompleteAdapter。
2.在调用autoCompleteAdapter =nameList(this,nameList)之前,确保数组列表有值;
发布于 2019-04-08 10:29:40
尝试使android失效并重新启动。
或
删除.gradle、.iml、.idea文件并重新构建项目
发布于 2019-04-08 11:35:51
您遇到了问题,因为您使用的是ArrayAdapter。ArrayAdapter保留自己的列表并对其进行处理,因此当您在构造函数中提供列表时,适配器将创建一个副本并使用该副本填充视图。更新list对象时,适配器的副本不会更新,但是由于重写了getCount方法并返回一个非零值,适配器尝试从空列表中获取项。
解决方案:您应该使用BaseAdapter代替。或者,如果您想使用ArrayAdapter,则应该使用ArrayAdapter.add()、ArrayAdapter.clear()等来修改适配器的列表。
https://stackoverflow.com/questions/55570662
复制相似问题