我正在尝试在列表视图中实现搜索功能,该功能显示在弹出警告对话框中。列表显示正常,搜索的Edittext也显示出来。但搜索功能根本不起作用。
我已经提到this tutorial来实现同样的目标。我想要实现的是,对话框将首先打开,而另一个函数将下载数据并填充适配器列表。下载数据后,列表将填充在对话框中。填充适配器并显示列表后,搜索应该有效。
显示弹出窗口的功能:
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();
}
从数据库接收数据的功能:
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);
}
}
});
}
填充列表的功能:
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();
}
}
}
}
适配器的代码:
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;
}
}
请让我知道我哪里出错了。为什么搜索不起作用?
答案 0 :(得分:0)
您可以使用适配器上的Filterable接口,参考此示例
答案 1 :(得分:0)
if (adapter != null)
方法中的showCollegePopUp()
检查可以避免添加文本更改侦听器。
根据提供的代码段,在执行null
检查时,您的适配器未初始化。
您应该在adapter
检查
null
答案 2 :(得分:0)
从下面更改编辑文字定义行
inputSearch =(EditText)findViewById(R.id.inputSearch);
到
inputSearch =(EditText)v.findViewById(R.id.inputSearch);