我使用MyCustom适配器在列表视图中填充文本和图像,它正确填充图像和文本但是 当我使用edittext过滤该列表视图时,它不会过滤。 请给我一些有用的代码和建议来解决这个问题
我的漏洞代码如下。
private ArrayList<String> friends;
private ArrayList<Bitmap> images;
ArrayAdapter<String> adapter;
SimpleCursorAdapter filterAdapter;
MyCustomAdapter dataAdapter = null;
ListView sp_make;
ImageView iv;
EditText etext;
// A good practice is to define database field names as constants
private static final String TABLE_NAME = "BREEDS";
private static final String BIKE_ID = "_id";
private static final String BIKE_MAKE = "Breeds";
private static final String AKCGROUP = "AKCGroup";
private static final String IMAGE = "image";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.allbreeds);
friends = new ArrayList<String>();
images = new ArrayList<Bitmap>();
BikeHelper dbOpenHelper = new BikeHelper(this, DB_NAME);
database = dbOpenHelper.openDataBase();
sp_make = (ListView) findViewById(R.id.listView1);
iv = (ImageView) findViewById(R.id.list_image);
fillFreinds();
fillFreindsImage();
dataAdapter = new MyCustomAdapter(this, R.layout.list_layout, friends);
dataAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp_make.setAdapter(dataAdapter);
sp_make.setFastScrollEnabled(true);
sp_make.setTextFilterEnabled(true);
etext = (EditText) findViewById(R.id.editText1);
etext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
MainActivity.this.dataAdapter.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
}
});
sp_make.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the
// result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
final String HorseCode = cursor.getString(cursor
.getColumnIndexOrThrow("DOG_ID"));
// Chp.setid(HorseCode);
// Intent signuplist = new Intent(SignupList.this,
// CheckProfile.class);
// startActivity(signuplist);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.new_bike, menu);
return true;
}
private void fillFreinds() {
Cursor friendCursor = database.query(TABLE_NAME,
new String[] { BIKE_MAKE }, null, null, null, null, BIKE_MAKE);
friendCursor.moveToFirst();
if (!friendCursor.isAfterLast()) {
do {
String name = friendCursor.getString(0);
friends.add(name);
} while (friendCursor.moveToNext());
}
friendCursor.close();
}
private void fillFreindsImage() {
Cursor friendCursor = database.query(TABLE_NAME,
new String[] { IMAGE }, null, null, null, null, null);
friendCursor.moveToFirst();
if (!friendCursor.isAfterLast()) {
do {
String name = friendCursor.getString(0);
// System.out.println("image name byte value==="+name);
byte[] decodedString = Base64.decode(name, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(
decodedString, 0, decodedString.length);
images.add(decodedByte);
System.out.println("image name byte value===" + decodedByte);
} while (friendCursor.moveToNext());
}
friendCursor.close();
}
protected class MyCustomAdapter extends ArrayAdapter<String>
// implements CompoundButton.OnCheckedChangeListener
{
ArrayList<String> myList;
int count = 0;
ViewHolder holder = null;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<String> sList) {
super(context, textViewResourceId, sList);
mCheckStates = new SparseBooleanArray(sList.size());
favourites = new SparseBooleanArray(sList.size());
myList = sList;
}
public void filter(CharSequence cs) {
// TODO Auto-generated method stub
}
private SparseBooleanArray mCheckStates;
private SparseBooleanArray favourites;
private class ViewHolder {
TextView text;
// CheckBox chkbox;
ImageView imageview;
}
@Override
public int getCount() {
return friends.size();
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_layout, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.name);
// holder.chkbox = (CheckBox)
// convertView.findViewById(R.id.checkBox1);
holder.imageview = (ImageView) convertView
.findViewById(R.id.list_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(friends.get(position).toString());
holder.imageview.setImageBitmap(images.get(position));
return convertView;
}
}
}