androidlistviewsearchsearchactivity

implement search in android


* this is my Activity *

I want to add search from the list of my items but cannot do so. See the code, if needed more then i will add more code.

    public class FacultyAndStaffActivity extends AppCompatActivity {
    ListView list_items;
    GuestTeacherAdapter guestTeacherAdapter;
    public ArrayList<HashMap<String, String>> tutorList;
    CC_Application ccApplication;
    LayoutInflater layoutInflater;
    Intent intent;
    String collegeId;
    android.widget.SearchView searchView = null;
    SessionManager sessionManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_faculty_and_staff);

        list_items = (ListView) findViewById(R.id.list_items);
        ccApplication = CC_Application.getInstance();
        layoutInflater = getLayoutInflater();
        intent = getIntent();
        sessionManager = new SessionManager(getApplicationContext());
        try {
            //saving college id in session testing
            if (sessionManager.isLoggedIn()){
                HashMap<String, String> user = sessionManager.getUserDetails();
                collegeId = user.get(SessionManager.KEY_COLLEGE_ID);
                tutorList = ccApplication.getAllTutorListByCollegeName(DbFields.TABLE_COLLEGE, DbFields.TABLE_TUTOR, DbFields.COLLEGE_ID, DbFields.T_COLLEGE_ID, collegeId);

            }else {
                if (intent != null) {
                    collegeId = intent.getStringExtra("id");
                    tutorList = ccApplication.getAllTutorListByCollegeName(DbFields.TABLE_COLLEGE, DbFields.TABLE_TUTOR, DbFields.COLLEGE_ID, DbFields.T_COLLEGE_ID, collegeId);
                }else {
                    Toast.makeText(FacultyAndStaffActivity.this, "No Intent received", Toast.LENGTH_SHORT).show();
                }
            }
        }catch (Exception e){
        }

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setCustomView(R.layout.actionbar_header);
        actionBar.setDisplayShowCustomEnabled(true);
        ImageView college_logo = (ImageView) findViewById(R.id.college_logo);
        ((TextView) findViewById(R.id.action_bar_title)).setText("Faculty & Staff By Name");
        college_logo.setBackgroundResource(R.drawable.faculty_staff);

        int checkValue = 1;

        guestTeacherAdapter = new GuestTeacherAdapter(this, layoutInflater, tutorList, checkValue);
        list_items.setAdapter(guestTeacherAdapter);
        list_items.setTextFilterEnabled(true);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_faq, menu);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            searchView = (android.widget.SearchView) menu.findItem(R.id.menu_item_search).getActionView();

            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false);
        } else {
            onSearchRequested();
        }return super.onCreateOptionsMenu(menu);
    }

* this is my adapter * this is adapter

    public class GuestTeacherAdapter extends BaseAdapter implements Filterable {

    GuestFacultyAndStaff pContext;
    LayoutInflater layoutInflater;
    DbFields dbFields = new DbFields();
    ArrayList<HashMap<String,String>> tutorList;
    Context mContext;
    int checkValue;

    public GuestTeacherAdapter(GuestFacultyAndStaff guestFacultyAndStaffFragment, LayoutInflater layoutInflater,
                               ArrayList<HashMap<String, String>> tutorListList) {
        this.pContext = guestFacultyAndStaffFragment;
        this.layoutInflater = layoutInflater;
        this.tutorList = tutorListList;
        this.notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        return tutorList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_teacher,null);
        }
        TextView teacherName = (TextView) convertView.findViewById(R.id.teacherName);
        //   TextView education = (TextView) convertView.findViewById(R.id.education);
        TextView phone = (TextView) convertView.findViewById(R.id.phoneNumber);
        TextView email = (TextView) convertView.findViewById(R.id.emailTeacher);
        TextView shotName = (TextView) convertView.findViewById(R.id.shotName);

        String teacherName1 = tutorList.get(position).get(dbFields.TUTOR_NAME);
        //  String education1 = tutorList.get(position).get(dbFields.TUTOR_EDUCATION);
        String phone1 = tutorList.get(position).get(dbFields.TUTOR_PHONE);
        String email1 = tutorList.get(position).get(dbFields.TUTOR_EMAIL);

        teacherName.setText(teacherName1);
        //   testEducation.setText(education1);
        phone.setText(phone1);
        email.setText(email1);
        shotName.setText(teacherName1.substring(0,1).toUpperCase());
        return convertView;
    }}

this adapter for listing details


Solution

  • implements SearchView.OnQueryTextListener in your activity and

    setup search view as :

      private void setupSearchView() {
            int id = mSearchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
            TextView textView = (TextView) mSearchView.findViewById(id);
            textView.setTextColor(Color.WHITE);
    
            mSearchView.setIconifiedByDefault(true);
            mSearchView.setOnQueryTextListener(this);
            mSearchView.setSubmitButtonEnabled(true);
            mSearchView.setQueryHint("Enter search");
    
        }
    
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    
                    @Override
                    public boolean onQueryTextSubmit(String query) {
    
                        return false;
                    }
    
                    @Override
                    public boolean onQueryTextChange(String searchQuery) {
                        myAppAdapter.filter(searchQuery.toString().trim());
                        listView.invalidate();
                        return true;
                    }
                });