这是我的代码
public class adapters extends Activity implements TextWatcher{
/** Called when the activity is first created. */
ListView lv1;
e E;
EditText e1;
LinearLayout ll1;
LinearLayout ll2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ll1.setBackgroundColor(color.transparent);
e1=(EditText)findViewById(R.id.e1);
lv1=(ListView)findViewById(R.id.lv1);
ArrayList<AdapterObject> obj = new ArrayList<AdapterObject>();
AdapterObject adp;
for(int i =0;i<5;i++){
adp = new AdapterObject();
adp.setBmp(getResources().getDrawable(R.drawable.trees));
adp.setStr("Text "+String.valueOf(i));
obj.add(adp);
}
E = new e(this,2,obj);
lv1.setAdapter(E);
lv1.setTextFilterEnabled(true);
e1.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
System.out.println(E.getItem(1));
E.getFilter().filter(e1.getText().toString(), new Filter.FilterListener() {
@Override
public void onFilterComplete(int count) {
// TODO Auto-generated method stub
System.out.println("tututututu");
E.notifyDataSetChanged();
}});
}
}
//here is the adapterobject class
public class AdapterObject {
Drawable bmp;
String str;
public Drawable getBmp() {
return bmp;
}
public void setBmp(Drawable bmp) {
this.bmp = bmp;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
//here is class e
public class e extends ArrayAdapter<AdapterObject> {
ArrayList<AdapterObject> Obj;
Context mContext;
public e(Context context, int textViewResourceId,
ArrayList<AdapterObject> objects) {
super(context, textViewResourceId, objects);
mContext=context;
Obj=objects;
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater l = (LayoutInflater) (mContext).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = l.inflate(R.layout.new21, null);
ImageView btn = (ImageView) v.findViewById(R.id.list_button);
TextView text = (TextView)v.findViewById(R.id.list_text);
btn.setBackgroundDrawable(Obj.get(position).getBmp());
text.setText(Obj.get(position).getStr());
System.out.println("here");
return v;
}
@Override
public AdapterObject getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
答案 0 :(得分:2)
edit=(EditText)findViewById(R.id.editText1);
edit.addTextChangedListener(filterTextWatcher);
private TextWatcher filterTextWatcher =new TextWatcher()
{
public void afterTextChanged(Editable s) {
mlist.getFilter().filter(edit.getText().toString());
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
public void onTextChanged(CharSequence s, int start, int before,int count) {
}
};
public class CustomAdapter extends ArrayAdapter<Employee> {
private ImageButton img;
private ArrayList<Employee> items;
private ArrayList<Employee> dupItems;
public ArrayList<Employee> filtered;
private Activity currActivity;
public Employee emp;
private EmpService empSer;
private Filter filter;
public CustomAdapter(Activity context, int textViewResourceId, ArrayList<Employee> empList) {
super(context, textViewResourceId, empList);
currActivity = context;
this.filtered=empList;
this.items = filtered;
setNotifyOnChange(true);
empSer = new EmpService();
emp=new Employee();
}
public int getCount()
{
return items.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) currActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.result, null);
}
emp = items.get(position);
view.setId(emp.empId);
if (emp != null) {
TextView id = (TextView) view.findViewById(R.id.textView1);
TextView name = (TextView) view.findViewById(R.id.textView2);
TextView com = (TextView) view.findViewById(R.id.textView3);
TextView joindate = (TextView) view.findViewById(R.id.textView4);
img = (ImageButton) view.findViewById(R.id.imageButton1);
if (id != null) {
id.setText(" "+emp.empId);
}
if(name != null){
name.setText(" "+emp.empName);
}
if(com != null){
com.setText(" "+emp.company);
}
if(joindate != null){
joindate.setText(" "+emp.joinDate);
}
return view;
}
public void notifyDataSetInvalidated()
{
super.notifyDataSetInvalidated();
}
@Override
public Filter getFilter()
{
if(filter == null)
filter = new MangaNameFilter();
return filter;
}
private class MangaNameFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
// NOTE: this function is *always* called from a background thread, and
// not the UI thread.
constraint = edit.getText().toString().toLowerCase();
FilterResults result = new FilterResults();
if(constraint != null && constraint.toString().length() > 0)
{
empList=empSer.GetAllDetails();
items=empList;
ArrayList<Employee> filt = new ArrayList<Employee>();
ArrayList<Employee> lItems = new ArrayList<Employee>();
synchronized(this)
{
lItems.addAll(items);
}
for(int i = 0, l = lItems.size(); i < l; i++)
{
Employee m = lItems.get(i);
if(m.empName.toLowerCase().contains(constraint)||m.joinDate.toLowerCase().contains(constraint)|| m.company.contains(constraint))
filt.add(m);
}
result.count = filt.size();
result.values = filt;
}
else
{
empList=empSer.GetAllDetails();
items=empList;
synchronized(this)
{
result.count = items.size();
result.values = items;
}
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults result) {
// NOTE: this function is *always* called from the UI thread.
filtered = (ArrayList<Employee>)result.values;
mlist=new CustomAdapter(currActivity, R.layout.result, filtered);
list.setAdapter(mlist);
}
}