2
本文是我的代碼在自定義列表視圖如何篩選數據
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);
}
在上面的代碼我正在充氣其中包含圖像和一個TextView – chinz 2011-04-06 11:03:53
當我試圖代碼與正常列表視圖的行它作品,但它不適用於cutom列表視圖 – chinz 2011-04-06 11:04:28