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?