I have created a list view in android and I want to add edit text above the list and when the user enter text the list will be filtered according to user input.listview in fragmentcode like below:
public class FindPeopleFragment extends BaseFragment implements LoaderCallbacks<Cursor> {
public FindPeopleFragment() {
}
private TextView advancedSearchOption;
private TextView saerchButton;
private EndlessListView listUsers;
private EditText searchWord;
private EditText searchFirstName;
private EditText searchLastName;
private EditText searchEmail;
private EditText searchCountry;
private EditText searchState;
private int offset = 0;
private final int limit = 30;
private boolean mHaveMoreDataToLoad = true;
private VROAccountManager manager;
private ArrayList<VROUser> postsList = new ArrayList<>();
private View advanced;
private Cursor FriendsCursor;
private Cursor FriendsCursorTemp;
// Cursor c;
private TextView description;
long friend_name;
FriendsListCursorAdapter FRIEND_ADAPTER;
DatabaseHelper dh;
FriendinfoService friendinfoService;
String searchValue;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
title = "My Friends";
// setSelection(0);
}
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
friendinfoService = new FriendinfoService();
if (rootView != null) {
((ViewGroup) rootView.getParent()).removeView(rootView);
return rootView;
}
rootView = inflater.inflate(R.layout.fragment_find_people, null);
getLoaderManager().initLoader(1, null, this);
// progressBar = rootView.findViewById(R.id.progressBar);
saerchButton = (TextView) rootView.findViewById(R.id.button_search);
listUsers = (EndlessListView) rootView.findViewById(R.id.user_list);
searchWord = (EditText) rootView
.findViewById(R.id.editText_search_word);
searchFirstName = (EditText) rootView
.findViewById(R.id.editText_search_first_name);
searchLastName = (EditText) rootView
.findViewById(R.id.editText_search_last_name);
searchEmail = (EditText) rootView
.findViewById(R.id.editText_search_email);
searchCountry = (EditText) rootView
.findViewById(R.id.editText_search_country);
searchState = (EditText) rootView
.findViewById(R.id.editText_search_state);
advancedSearchOption = (TextView) rootView
.findViewById(R.id.advancedSearch);
advancedSearchOption.setVisibility(View.GONE);
manager = new VROAccountManager(getActivity());
advanced = rootView.findViewById(R.id.layout_advanced_search);
description = (TextView) rootView.findViewById(R.id.description);
dh=new DatabaseHelper(getActivity());
description
.setText("You are friends with the following users. To find new friends or to view friends request sent or received use the menu on right");
searchWord.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
FindPeopleFragment.this.FRIEND_ADAPTER.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
listUsers.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor mycursor = (Cursor) listUsers.getItemAtPosition(position);
VROUser user = VROUser.parse(mycursor);
Intent intent = new Intent(getActivity(), ProfileViewActivity.class);
intent.putExtra("userId", user.getUserID());
getActivity().startActivity(intent);
}
});
return rootView;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader cursorLoader=null;
if(id==1) {
cursorLoader = new CursorLoader(getActivity(),
FriendinfoTable.CONTENT_URI, null, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
System.out.println("Value returningnnnnnnnnnnnnnnnnnnnnnnnnnnn " + data.getCount());
FriendsCursor = data;
if (FRIEND_ADAPTER == null) {
FRIEND_ADAPTER = new FriendsListCursorAdapter(getActivity(),
FriendsCursor, true);
listUsers.setAdapter(FRIEND_ADAPTER);
FRIEND_ADAPTER = null;
} else {
FRIEND_ADAPTER.swapCursor(FriendsCursor);
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
}
and CursorAdapter is coded as
public class FriendsListCursorAdapter extends CursorAdapter implements Filterable {
private Cursor cursor;
final VROPreferenceManager preferenceManager = new VROPreferenceManager();
Context con;
String cString;
List<String> arrayListNames;
public FriendsListCursorAdapter(Context context, Cursor c, boolean b) {
super(context,c,true);
cursor = c;
con=context;
}
public void addItems(Cursor newItems) {
if (0 == newItems.getCount()) {
return;
}
notifyDataSetChanged();
}
public int getCount() {
// TODO Auto-generated method stub
return cursor.getCount();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.vro_contact_view, null);
bindView(view, context, cursor);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView txtID = (TextView) view.findViewById(R.id.text_fname_lname);
txtID.setText(cursor.getString(cursor.getColumnIndex("first_name")));
TextView txtCode = (TextView) view.findViewById(R.id.text_email);
txtCode.setText(cursor.getString(cursor.getColumnIndex("email")));
ImageView profilePic = (ImageView) view.findViewById(R.id.user_image);
VROUser pic = VROUser.parse(cursor);
ImageLoader.getInstance().displayImage(
preferenceManager.getResourceSertver()
+ pic.getProfilePICURL(), profilePic,
Consts.UIL_USER_AVATAR_DISPLAY_OPTIONS);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results = new FilterResults();
ArrayList<String> FilteredArrayNames = new ArrayList<String>();
// perform your search here using the searchConstraint String.
cString = charSequence.toString().toLowerCase();
for (int i = 0; i < cursor.getCount(); i++) {
String dataNames = cursor.getString(i);
if (dataNames.toLowerCase().startsWith(cString.toString())) {
FilteredArrayNames.add(dataNames);
}
}
results.count = FilteredArrayNames.size();
results.values = FilteredArrayNames;
// Log.e("VALUES", results.values.toString());
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
arrayListNames = (List<String>) filterResults.values;
notifyDataSetChanged();
}
};
return filter;
}
}
but i got an error like java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.Filter vro.nagainfo.vromain.ui.adaptors.FriendsListCursorAdapter.getFilter()' on a null object reference at line
FindPeopleFragment.this.FRIEND_ADAPTER.getFilter().filter(cs);
please help me to find the solution.