我有一个AutocompleteTextView
,为此我编写了以下适配器。我想从Web API获取数据,这可以正常工作,并且我可以从服务器获取数据。当我在autocompleteTextView
上写文本时,以下代码显示以下错误。我调试代码,在调用notifyDataSetChanged
和调用getViewMethod
之后显示此错误。我不知道我的问题是什么。
[另一件事,我从服务器获取了一个对象列表(我的意思是像name,family,id),我只想显示其中一部分(我是想显示名称),为此,我创建了自定义适配器。
我的适配器:
public class DropDownAdapter extends BaseAdapter implements Filterable {
private static final int MAX_RESULTS = 10;
private Context mContext;
private List<TestModel> resultList = new ArrayList<TestModel>();
private boolean placeResults = false;
private Object lockTwo = new Object();
private Object lock = new Object();
public DropDownAdapter(Context context) {
mContext = context;
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public Object getItem(int position) {
return resultList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View v = view;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.one_row_dropdown, viewGroup, false);
}
TextView textView = v.findViewById(R.id.textview);
textView.setText(resultList.get(position).code);
return view;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults filterResults = new FilterResults();
if (constraint == null || constraint.length() == 0) {
synchronized (lock) {
filterResults.values = new ArrayList<String>();
filterResults.count = 0;
}
} else {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("token", OfflineData.getGUID(mContext));
jsonObject.put("code", constraint.toString());
jsonObject.put("state", false);
} catch (JSONException e) {
e.printStackTrace();
}
Type type = new TypeToken<ServiceResult<List<TestModel>>>() {
}.getType();
WebService getBranchCodeListWebService = new WebService(AppConstant.URL_LISTINGDATA, "TestMethod", type);
getBranchCodeListWebService.addOnCompletedCallBack(new WebService.OnCompletedCallBack() {
@Override
public void onCompletedCallBack(boolean isSucceed, Object returnedObject) {
if (isSucceed) {
if (returnedObject != null) {
ServiceResult<List<TestModel>> response = (ServiceResult<List<TestModel>>) returnedObject;
if (response.IsSuccess) {
if (response.Result.size() > 0) {
resultList.clear();
resultList = response.Result;
filterResults.values = resultList;
filterResults.count = resultList.size();
}
}
}
}
placeResults = true;
synchronized (lockTwo) {
lockTwo.notifyAll();
}
}
});
getBranchCodeListWebService.execute(jsonObject);
while (!placeResults) {
synchronized (lockTwo) {
try {
lockTwo.wait();
} catch (InterruptedException ex) {
}
}
}
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
错误结果:
Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference at android.widget.AbsListView.obtainView(AbsListView.java:2382) at android.widget.DropDownListView.obtainView(DropDownListView.java:305) at android.widget.ListView.measureHeightOfChildren(ListView.java:1408) at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1257) at android.widget.ListPopupWindow.show(ListPopupWindow.java:613) at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1217) at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:1086) at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:1068) at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
在MainActivity中创建适配器:
autoCompleteTextView = findViewById(R.id.branchCodeEdittextAutocomplete); autoCompleteTextView.setThreshold(2);
DropDownAdapter dropDownAdapter=new DropDownAdapter(this);
autoCompleteTextView.setAdapter(dropDownAdapter);
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//go to next page... or anything like this
}
});
将return view
更改为return v
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View v = view;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.one_row_dropdown, viewGroup, false);
}
TextView textView = v.findViewById(R.id.textview);
textView.setText(resultList.get(position).code);
return v;
}