I have an AutocompleteTextView
and I wrote the below adapter for that. I want to get data from Web API and this works correctly and I can to get data from the server. The below code shows the below error when I write a text on autocompleteTextView
. I debug my code, this error shows after call notifyDataSetChanged
and call getViewMethod
. I don't know what is my problem.
Another thing, I get a list of objects from the server (I mean like name,family,id) and I want to show just one part of that (I mean, I want to show name) and for this, I create a Custom adapter.
My Adapter:
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;
}
Error Result:
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)
Create adapter in 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
}
});
You are not returning the correct view,Change your return view
to 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;
}
and you don't have to assign view every time you can use view
it will hold the reference of your view in the adapter, so you can remove the line View v = view;
and replace your code as
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
if (view == 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;
}
Hope this helps...