cursorloader selection query using like

1.2k views Asked by At

I am doing search functionality using CursorLoader class. i want to check search word present in first name or in second name.

loader fragment

public class FriendsListCursorAdapter extends CursorAdapter  {
    private Cursor cursor;
    final VROPreferenceManager preferenceManager = new VROPreferenceManager();
    Context con;
    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();
    }

cursorLoADER FRAGMENT 



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;

    // 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);
        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");


        saerchButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
searchValue=searchWord.getText().toString();
                getLoaderManager().initLoader(2, null, FindPeopleFragment.this);




            }
        });
        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;
    }




    private final EndlessListView.OnLoadMoreListener loadMoreListener = new EndlessListView.OnLoadMoreListener() {

        @Override
        public boolean onLoadMore() {
            if (true == mHaveMoreDataToLoad) {
                //loadMoreData();
                getLoaderManager().initLoader(1, null, FindPeopleFragment.this);
            } else {
                Toast.makeText(getActivity(), "No more data to load",
                        Toast.LENGTH_SHORT).show();
            }

            return mHaveMoreDataToLoad;
        }

        @Override
        public void onReachedTop(boolean isTop) {
            // TODO Auto-generated method stub

        }
    };
    private final Handler handler = new Handler();
    private View progressBar;




    @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);
 }
              FriendinfoTable.Cols.FRIENDSHIP_STATUS, FriendinfoTable.Cols.QUICKBLOX_ID};

if (id==2) {


           FriendinfoTable.CONTENT_URI, null, selectQuery, null, null);

    cursorLoader = new CursorLoader(getActivity(),
            FriendinfoTable.CONTENT_URI, null, FriendinfoTable.Cols.FIRST_NAME+" like '%"+searchValue+"%' or "+FriendinfoTable.Cols.LAST_NAME+" like '%"+searchValue+"%'",null, null);

                     }

        return cursorLoader;
    }


    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        System.out.println("Value returningggggggggggggggg" + data.getCount());
        FriendsCursor = data;
try {
    if (FRIEND_ADAPTER == null) {
        FRIEND_ADAPTER = new FriendsListCursorAdapter(getActivity(),
                FriendsCursor, true);
        listUsers.setAdapter(FRIEND_ADAPTER);
    } else {
        FRIEND_ADAPTER.swapCursor(FriendsCursor);

    }
}
   catch (Exception E)
   {
       Log.i("ERROR",E.getMessage());
   }

    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }
}





        @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);


        }



    }
FriendInfoTable is a class where table and fields are created look like

public class FriendinfoTable {
    public static final String TABLE_NAME = "friendinfotable";



    public static class Cols {


        public static final String ID = "_id";
        public static final String FIRST_NAME = "first_name";
        public static final String LAST_NAME = "last_name";
        public static final String USER_NAME = "user_name";
        public static final String EMAIL="email";
        public static final String USER_PROFILE_PIC="user_profile_pic";
        public static final String USER_TIMELINE_PIC="user_timeline_pic";
        public static final String COUNTRY="country";
        public static final String STATE="state";
        public static final String CITY="city";
        public static final String ABOUT="about";
        public static final String GENDER="gender";
        public static final String DATEOFBIRTH="dob"; 
        public static final String FRIENDSHIP_STATUS="friendship_status"; 
        public static final String QUICKBLOX_ID="quickblox_id"; 

                            }


}

this is complete code..Listview is success before search function. please help me

1

There are 1 answers

12
Kartheek On

For this you can use like for check a work in first and last name.

cursorLoader = new CursorLoader(getActivity(),
                    FriendinfoTable.CONTENT_URI, null, FIRST_NAME+" like '%"+search_work+"%' or "+SECOND_NAME+" like '%"+search_work+"%'", null, null);