0

I am trying to implement a search functionality in a listview which is displayed inside a popup alert dialog. The list shows fine and the Edittext for the search is also showing up. But the search function is not working as at all.

I had referred this tutorial to implement the same. What I want to achieve is that, the dialog will open first, while another function will download the data and populate the adapter for the list. Once the data is downloaded the list will be populated inside the dialog. The search should work once the adapter is populated and list shows up.

The function to show the popup:

private void showCollegePopUp(){
        QustomDialogBuilder builder = new QustomDialogBuilder(EditYourProfile.this);
        builder.setDividerColor(ColorController.bright_green);

        View v = builder.setCustomView(R.layout.dialog_friend_layout, this);

        final ListView list = (ListView) v.findViewById(R.id.dialog_list_view_friends);
        LayoutInflater inflater = (LayoutInflater)EditYourProfile.this.getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);
        footerView = inflater.inflate(R.layout.add_college_list_footer, list, false);
        list.addFooterView(footerView);
        inputSearch = (EditText) findViewById(R.id.inputSearch);
        if (adapter != null) {
            inputSearch.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                    EditYourProfile.this.adapter.getFilter().filter(cs);
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                              int arg3) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                }
            });
        }
        TextView textView = (TextView) v.findViewById(R.id.add_college);

        textView.setTypeface(TypeFaceController.generalTextFace(EditYourProfile.this));

        LinearLayout footer_linear_layout = (LinearLayout)v.findViewById(R.id.footer_linear_layout);
        footer_linear_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(EditYourProfile.this, "hello", Toast.LENGTH_LONG).show();
            }
        });
        footerView.setVisibility(View.GONE);

        if (listOfCollegeCourseNames.size() == 0) {
            listOfCollegeCourseNames.add("Grabbing colleges...");
        }

        adapter = new CustomDialogAdapterBasic(EditYourProfile.this, android.R.id.text1, listOfCollegeCourseNames);
        list.setPadding(16, 0, 0, 0);
        list.setAdapter(adapter);

        builder.setTitle("Select your college");
        builder.setMessage("Choose College from the list:");
        builder.setCancelable(true);

        builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1){

            }
        });

        list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                // Do as you please

                if (adapter.getItem(position).toString().equals(collegeData.get(position).getNameForCollege())
                        || adapter.getItem(position).toString().equals(collegeData.get(position).getStudentsNameForCollege())) {

                    newCollegeName = adapter.getItem(position).toString();

                    collegeEditPage.setText(Html.fromHtml((newCollegeName)));// +
                                                                                // edit));
                    //courseEditPage.setText(Html.fromHtml(("Must choose new course")));// +
                                                                                        // edit));
                    // course doesn't exist anymore.
                    studentObject.setCourseName(null);
                    studentObject.setCollegeName(newCollegeName);

                    if (EditYourProfile.this.alertDialog != null) {
                        EditYourProfile.this.alertDialog.dismiss();
                        // Refresh the list used.
                        listOfCollegeCourseNames = new ArrayList<String>();
                        newCollegeId = collegeData.get(position).getCollegeUnqId();
                        studentObject.setCollegeId(newCollegeId);
                    }

                }

            }
        });

        this.alertDialog = builder.create();
        this.alertDialog.setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog){
                AlertDialog alertDialog = (AlertDialog) dialog;
                Button button = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
                button.setTextSize(17);
                button.setTypeface(TypeFaceController.titleFace(getApplicationContext()));
            }
        });
        alertDialog.show();

    }

The function to receive the data from the database:

private void fetchAllCollegesAndDisplay(){
        final List<College> collegeDetailList = new ArrayList<College>();
        ParseQuery<ParseObject> query = ParseQuery.getQuery("Colleges");
        query.addAscendingOrder("name");

        query.findInBackground(new FindCallback<ParseObject>() {

            @Override
            public void done(List<ParseObject> objects, com.parse.ParseException e){
                if (e == null) {
                    Log.e("Objects size", "" + objects.size());
                        for (int i = 0; i < objects.size(); i++) {
                            if (objects.get(i).get("status") != null) {
                                if (objects.get(i).getBoolean("status") == true){
                                    College college = new College();
                                    college.setNameForCollege(objects.get(i).get("name").toString());
                                    college.setCollegeUnqId(objects.get(i).getObjectId()); // Grab
                                    collegeDetailList.add(college);
                                }

                            }
                        }
                    generateList(collegeDetailList);

                }

            }

        });
    }

The function to populate the list:

private void generateList(List<?> collegeOrCourseList){
        listOfCollegeCourseNames.remove(0);

        if (collegeOrCourseList.size() != 0) {

            // Check if the list is of type College.
            if (collegeOrCourseList.get(0) instanceof College) {
                for (Object c : collegeOrCourseList) {

                    if (((College) c).getStudentsNameForCollege() != null) {
                        //listOfCollegeCourseNames.add(((College) c).getStudentsNameForCollege());
                        listOfCollegeCourseNames.add(((College) c ).getNameForCollege());
                    } else

                    if (((College) c).getNameForCollege() != null) {
                        listOfCollegeCourseNames.add(((College) c).getNameForCollege());
                    }

                    collegeData.add((College) c);
                    adapter.notifyDataSetChanged();

                }
                footerView.setVisibility(View.VISIBLE);
            }

            if (collegeOrCourseList.get(0) instanceof Course) {
                courseData.clear();
                for (Object c : collegeOrCourseList) {
                    listOfCollegeCourseNames.add(((Course) c).getCourse());
                    courseData.add((Course) c);
                    adapter.notifyDataSetChanged();

                }
            }

        }
    }

The adapter's code:

public class CustomDialogAdapterBasic extends ArrayAdapter<String> {

    Context context;
    List<String> valuesComingIn = new ArrayList<String>();



    public CustomDialogAdapterBasic(Context context, int resource, List<String> listComingIn) {
        super(context, resource);
        this.context = context;
        this.valuesComingIn = listComingIn;
    }


    public void updateBrowser() {
        this.notifyDataSetChanged();
    }

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

    public String getItem(int position) throws IndexOutOfBoundsException {
        return valuesComingIn.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean isEnabled(int position) {
        return true;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.qustom_layout_list, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.basic_text_view);

        textView.setTypeface(TypeFaceController.generalTextFace(context));
        textView.setText(getItem(position));

        return rowView;
    }
}

Please let me know where I am going wrong. Why is the search not working?

kittu88
  • 2,451
  • 5
  • 40
  • 80

3 Answers3

0

You can use the Filterable interface on your Adapter, refer this example

Custom Listview Adapter with filter Android hope it will work for you

Community
  • 1
  • 1
Ajinkya
  • 1,057
  • 7
  • 18
  • Please check the output in http://stackoverflow.com/questions/35766258/implementing-filterable-in-custom-adapter-does-not-perform-search-in-listview with Filterable implementation – kittu88 Mar 03 '16 at 07:57
0

The check if (adapter != null) in your showCollegePopUp()method avoid the addition of your text change listener.

According to the provided piece of code, your adapter is not initialized when null check is performed.

You should initialize the adapter before the null check

dishan
  • 1,346
  • 12
  • 21
  • I tried to initialise the search after adapter.notifyDataSetChanged(); still the same error shows! – kittu88 Mar 03 '16 at 05:28
  • I am getting java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference – kittu88 Mar 03 '16 at 05:36
  • It's not clear to me what change you did. Did you initialize the adapter before the call to `inputSearch.addTextChangedListener` ? – dishan Mar 03 '16 at 05:47
  • yeah the adapter is initialise before, but the adapter gets updated later once – kittu88 Mar 03 '16 at 06:05
  • Can't say a much with the available information. Seems your `inputSearch` is `null`. Need the logcat output for the exception for further troubleshoot – dishan Mar 03 '16 at 06:11
0

Change your edit text definition line from below

 inputSearch = (EditText) findViewById(R.id.inputSearch);

to

 inputSearch = (EditText) v.findViewById(R.id.inputSearch);
Saeed
  • 3,294
  • 5
  • 35
  • 52
Meenal
  • 2,879
  • 5
  • 19
  • 43