Autocompletetextview:java.lang.IllegalStateException:adapter的内容已经改变但是ListView没有收到通知
Autocompletetextview:java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification
当我点击 autocompletetextview.I 中的某个项目时,我有时会遇到异常,这不是 运行 这在后台线程中,我正在使用 notifyDataSetChanged() also.I 无法理解在哪里我错了吗
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes.
代码
searchautocomplete.setAdapter(searchAutoCompleteAdapter= new SearchAutoCompleteAdapter(getApplicationContext(),searchautocomplete.getText().toString(),this)); // 'this' is Activity instance
searchautocomplete.setLoadingIndicator(
(android.widget.ProgressBar) findViewById(R.id.pb_loading_indicator));
searchautocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
UserVideo data=(UserVideo)parent.getItemAtPosition(position);
searchautocomplete.setText("");
if ((data.getIsVideo()))
{
searchAutoCompleteAdapter.notifyDataSetChanged();
setSearchVideoToShow(data);
mSearchLayout.setVisibility(View.GONE);
switchFragment(HomeActivity.FRAGMENT_VIDEO_SEARCH, false, "Video Details");
}
else if ((!data.getIsVideo()))
{
searchAutoCompleteAdapter.notifyDataSetChanged();
//mSingleClickHandle.put(ENABLE_FRAGMENT_SEARCH_VIEW, false);
setUserIdToShow(data.getUserId2());
closeDrawer();
mSearchLayout.setVisibility(View.GONE);
switchFragment(HomeActivity.FRAGMENT_PROFILE_VIEW, false, "video");
}
}
});
适配器
public class SearchAutoCompleteAdapter extends BaseAdapter implements Filterable {
private static final int MAX_RESULTS = 10;
private Context mContext;
private String searchString;
List<UserObject> mUserList;
List<UserObject> userList;
HomeActivity mHomeActivity;
String url;
private ArrayList<UserVideo> searchResponses=new ArrayList<UserVideo>();
private ArrayList<UserVideo> searchUserResponses=new ArrayList<UserVideo>();
public SearchAutoCompleteAdapter(Context c,String text,HomeActivity homeActivity) {
mContext=c;
searchString=text;
mHomeActivity=homeActivity;
}
@Override
public int getCount() {
return searchResponses.size();
}
@Override
public UserVideo getItem(int index) {
return searchResponses.get(index);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_search_view, parent, false);
}
NetworkImageView userImageView = (NetworkImageView)convertView.findViewById(R.id.search_user_image);
ImageView searchUser= (ImageView)convertView.findViewById(R.id.search_user);
ImageView searchVideo= (ImageView)convertView.findViewById(R.id.search_video);
TextView userNameView = (TextView)convertView.findViewById(R.id.search_user_handle);
TextView nameTextView = (TextView)convertView.findViewById(R.id.name);
if(getItem(position).getIsVideo())
{
searchVideo.setVisibility(View.VISIBLE);
searchUser.setVisibility(View.GONE);
nameTextView.setText(searchResponses.get(position).getCaption());
userNameView.setText(searchResponses.get(position).getLocation());
url=TremoURLs.VIDEO_THUMBNAIL_URL.replace("<fixme>", getItem(position).getId());
LoadImage.loadImage(url, userImageView, R.drawable.default_video_img);
}
else
{
url= TremoURLs.USER_PROFILE_IMAGE_URL + getItem(position).getUserId2();
searchUser.setVisibility(View.VISIBLE);
searchVideo.setVisibility(View.GONE);
Log.e("bhuvnesh ","pos "+position);
nameTextView.setText(searchResponses.get(position).getUserName());
userNameView.setText(searchResponses.get(position).getUserHandle());
LoadImage.loadImage(url, userImageView, R.drawable.default_video_img);
}
return convertView;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint.length() > 2)
{
ArrayList<UserVideo> users = findResult(mContext, constraint.toString());
filterResults.values = users;
filterResults.count = users.size();
}
//notifyDataSetChanged();
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
if (results != null && results.count > 0)
{
searchResponses = (ArrayList<UserVideo>) results.values;
notifyDataSetChanged();
}
else
{
notifyDataSetInvalidated();
}
}
};
return filter;
}
private ArrayList<UserVideo> findResult(final Context context, final String user)
{
SearchAPI.getSearchFeed(context,user,"", 10, mHomeActivity.mSettingsManager.getAccessToken(), new APIResponseListener() {
@Override
public void onResponse(Object response) {
searchResponses=(ArrayList<UserVideo>) response;
for(UserVideo userVideo:searchResponses)
{
userVideo.setIsVideo(true);
}
SearchAPI.getSearchUser(context,user,mHomeActivity.mSettingsManager.getAccessToken(), new APIResponseListener() {
@Override
public void onResponse(Object response)
{
searchUserResponses=(ArrayList<UserVideo>) response;
for(UserVideo userVideo:searchUserResponses)
{
userVideo.setIsVideo(false);
}
searchResponses.addAll(searchUserResponses);
}
@Override
public void onError(VolleyError error) {
}
});
}
@Override
public void onError(VolleyError error) {
}
});
return searchResponses;
}
}
添加notifyDataSetChanged()
;在我内部 API 调用的 onResponse() 内部解决了我的问题..
当我点击 autocompletetextview.I 中的某个项目时,我有时会遇到异常,这不是 运行 这在后台线程中,我正在使用 notifyDataSetChanged() also.I 无法理解在哪里我错了吗
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes.
代码
searchautocomplete.setAdapter(searchAutoCompleteAdapter= new SearchAutoCompleteAdapter(getApplicationContext(),searchautocomplete.getText().toString(),this)); // 'this' is Activity instance
searchautocomplete.setLoadingIndicator(
(android.widget.ProgressBar) findViewById(R.id.pb_loading_indicator));
searchautocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
UserVideo data=(UserVideo)parent.getItemAtPosition(position);
searchautocomplete.setText("");
if ((data.getIsVideo()))
{
searchAutoCompleteAdapter.notifyDataSetChanged();
setSearchVideoToShow(data);
mSearchLayout.setVisibility(View.GONE);
switchFragment(HomeActivity.FRAGMENT_VIDEO_SEARCH, false, "Video Details");
}
else if ((!data.getIsVideo()))
{
searchAutoCompleteAdapter.notifyDataSetChanged();
//mSingleClickHandle.put(ENABLE_FRAGMENT_SEARCH_VIEW, false);
setUserIdToShow(data.getUserId2());
closeDrawer();
mSearchLayout.setVisibility(View.GONE);
switchFragment(HomeActivity.FRAGMENT_PROFILE_VIEW, false, "video");
}
}
});
适配器
public class SearchAutoCompleteAdapter extends BaseAdapter implements Filterable {
private static final int MAX_RESULTS = 10;
private Context mContext;
private String searchString;
List<UserObject> mUserList;
List<UserObject> userList;
HomeActivity mHomeActivity;
String url;
private ArrayList<UserVideo> searchResponses=new ArrayList<UserVideo>();
private ArrayList<UserVideo> searchUserResponses=new ArrayList<UserVideo>();
public SearchAutoCompleteAdapter(Context c,String text,HomeActivity homeActivity) {
mContext=c;
searchString=text;
mHomeActivity=homeActivity;
}
@Override
public int getCount() {
return searchResponses.size();
}
@Override
public UserVideo getItem(int index) {
return searchResponses.get(index);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_search_view, parent, false);
}
NetworkImageView userImageView = (NetworkImageView)convertView.findViewById(R.id.search_user_image);
ImageView searchUser= (ImageView)convertView.findViewById(R.id.search_user);
ImageView searchVideo= (ImageView)convertView.findViewById(R.id.search_video);
TextView userNameView = (TextView)convertView.findViewById(R.id.search_user_handle);
TextView nameTextView = (TextView)convertView.findViewById(R.id.name);
if(getItem(position).getIsVideo())
{
searchVideo.setVisibility(View.VISIBLE);
searchUser.setVisibility(View.GONE);
nameTextView.setText(searchResponses.get(position).getCaption());
userNameView.setText(searchResponses.get(position).getLocation());
url=TremoURLs.VIDEO_THUMBNAIL_URL.replace("<fixme>", getItem(position).getId());
LoadImage.loadImage(url, userImageView, R.drawable.default_video_img);
}
else
{
url= TremoURLs.USER_PROFILE_IMAGE_URL + getItem(position).getUserId2();
searchUser.setVisibility(View.VISIBLE);
searchVideo.setVisibility(View.GONE);
Log.e("bhuvnesh ","pos "+position);
nameTextView.setText(searchResponses.get(position).getUserName());
userNameView.setText(searchResponses.get(position).getUserHandle());
LoadImage.loadImage(url, userImageView, R.drawable.default_video_img);
}
return convertView;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint.length() > 2)
{
ArrayList<UserVideo> users = findResult(mContext, constraint.toString());
filterResults.values = users;
filterResults.count = users.size();
}
//notifyDataSetChanged();
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
if (results != null && results.count > 0)
{
searchResponses = (ArrayList<UserVideo>) results.values;
notifyDataSetChanged();
}
else
{
notifyDataSetInvalidated();
}
}
};
return filter;
}
private ArrayList<UserVideo> findResult(final Context context, final String user)
{
SearchAPI.getSearchFeed(context,user,"", 10, mHomeActivity.mSettingsManager.getAccessToken(), new APIResponseListener() {
@Override
public void onResponse(Object response) {
searchResponses=(ArrayList<UserVideo>) response;
for(UserVideo userVideo:searchResponses)
{
userVideo.setIsVideo(true);
}
SearchAPI.getSearchUser(context,user,mHomeActivity.mSettingsManager.getAccessToken(), new APIResponseListener() {
@Override
public void onResponse(Object response)
{
searchUserResponses=(ArrayList<UserVideo>) response;
for(UserVideo userVideo:searchUserResponses)
{
userVideo.setIsVideo(false);
}
searchResponses.addAll(searchUserResponses);
}
@Override
public void onError(VolleyError error) {
}
});
}
@Override
public void onError(VolleyError error) {
}
});
return searchResponses;
}
}
添加notifyDataSetChanged()
;在我内部 API 调用的 onResponse() 内部解决了我的问题..