问题
I create custom listView
for contacts and EditText
for search contents.
Here is search code.
Main Activity
EditText contactSearch = (EditText) findViewById(R.id.contactSearch);
contactSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
contentAdapter.getFilter().filter(s);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
contentAdapter
is static variable which is define in another class which is in different file this class is extend from AysnTask
BackgroundWorker
public static ContentAdapter contentAdapter;
// other code
contentAdapter = new ContentAdapter(context, names, phones);
listView.setAdapter(contentAdapter);
I make it static variable because I want to access it from mainActivity class as define above.
There is no error But search functionality does not work properly. I try to search contact but it can't search. I don't know what the problem is. There is no error but searching is not working fine.
UPDATE ContentAdapter Class
public class ContentAdapter extends ArrayAdapter<String> {
private Context context;
private String[] names;
private String[] phones;
public static Dialog dialog;
ContentAdapter(Context ctx, String[] name, String[] phone){
super(ctx, R.layout.contact_row,R.id.txtName,name);
context = ctx;
names = name;
phones = phone;
dialog = new Dialog(context);
}
private class ViewHolder{
TextView name, phone;
ViewHolder(View view){
name = (TextView) view.findViewById(R.id.txtName);
phone = (TextView) view.findViewById(R.id.txtPhoneNumber);
view.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(context, "Item click", Toast.LENGTH_SHORT).show();
String phoneNumber = phone.getText().toString();
String userName = name.getText().toString();
//final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle(userName);
EditText etxtContactNumber = (EditText) dialog.findViewById(R.id.etxtContactNumber);
etxtContactNumber.setText(phoneNumber);
dialog.show();
}
});
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder viewHolder = null;
if(row == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.contact_row, parent, false);
viewHolder = new ViewHolder(row);
row.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) row.getTag();
}
viewHolder.name.setText(names[position]);
viewHolder.phone.setText(phones[position]);
return row;
}
}
回答1:
Use this code in adapter.
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
names.clear();
if (charText.length() == 0) {
names.addAll(arraylist);
} else {
for (Names wp : arraylist) {
if (wp.toLowerCase(Locale.getDefault())
.contains(charText)) {
names.add(wp);
}
}
}
notifyDataSetChanged();
}
来源:https://stackoverflow.com/questions/42503466/add-search-functionality-to-custom-list-view