我在一个具有搜索功能的对话框中有一个简单的列表视图。搜索功能正常,但列表的setOnItemClickListener在执行搜索后停止工作。
创建列表视图的功能:
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)v.findViewById(R.id.inputSearch);
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) {
alertDialog.dismiss();
Intent i = new Intent(EditYourProfile.this, CreateCollegeActivity.class);
startActivity(i);
}
});
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);
inputSearch.addTextChangedListener(new TextWatcher() {
//Event when changed word on EditTex
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e("Text", "Text [" + s + "]");
adapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
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();
}
Listview的支持搜索功能的适配器:
public class CustomDialogAdapterBasic extends ArrayAdapter<String> implements Filterable {
Context context;
List<String> valuesComingIn = new ArrayList<String>();
List<String> valuesFiltered = new ArrayList<String>();
private ItemFilter mFilter = new ItemFilter();
public CustomDialogAdapterBasic(Context context, int resource, List<String> listComingIn) {
super(context, resource);
this.context = context;
this.valuesComingIn = listComingIn;
this.valuesFiltered = listComingIn;
}
public void updateBrowser() {
this.notifyDataSetChanged();
}
@Override
public int getCount() {
return valuesFiltered.size();
}
public String getItem(int position) throws IndexOutOfBoundsException {
return valuesFiltered.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;
}
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
Log.e("filterString", filterString);
FilterResults results = new FilterResults();
final List<String> list = valuesComingIn;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
valuesFiltered = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
列表视图行布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/basic_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_margin="8dp"
android:text="TextView" />
</LinearLayout>
包含listview的对话框的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search colleges.."
android:inputType="textVisiblePassword"/>
<ListView
android:id="@+id/dialog_list_view_friends"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
我哪里错了?为什么搜索后setOnItemClickListener
无效?
答案 0 :(得分:0)
Try this way to implement clickListener
public class CustomDialogAdapterBasic extends ArrayAdapter<String> implements Filterable {
Context context;
List<String> valuesComingIn = new ArrayList<String>();
List<String> valuesFiltered = new ArrayList<String>();
private ItemFilter mFilter = new ItemFilter();
public CustomDialogAdapterBasic(Context context, int resource, List<String> listComingIn) {
super(context, resource);
this.context = context;
this.valuesComingIn = listComingIn;
this.valuesFiltered = listComingIn;
}
public void updateBrowser() {
this.notifyDataSetChanged();
}
@Override
public int getCount() {
return valuesFiltered.size();
}
public String getItem(int position) throws IndexOutOfBoundsException {
return valuesFiltered.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));
textView.setOnClickListener(new Listener(getItem(position)));
return rowView;
}
public Filter getFilter() {
return mFilter;
}
class Listener implements View.OnClickListener
{
String textData;
Listener(String textData)
{
this.textData = textData;
}
@Override
public void onClick(View v) {
// Implement your click functionality here and call any method of activity like this
// ((ActivityName)context).methodName();
}
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
Log.e("filterString", filterString);
FilterResults results = new FilterResults();
final List<String> list = valuesComingIn;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
valuesFiltered = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
答案 1 :(得分:0)
尝试添加
android:descendantFocusability="blocksDescendants"
在XML的父布局中。