我在自定义列表视图中有一个复选框和一个textview。如果我从edittext搜索,我如何过滤比较editext值和textview的适配器。 下面的代码给出了所有过滤的内容。也就是说,它与我在Edittext中输入的文本和Textview中的文本进行比较。任何帮助都很明显。
public class markblood extends Activity {
SQLiteDatabase db;
MyCustomAdapter dataAdapter = null;
EditText inputSearch;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.markblood);
db=openOrCreateDatabase("blood", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS donors(name VARCHAR,nick VARCHAR,place VARCHAR,city VARCHAR,phone VARCHAR,bgroup VARCHAR,mark VARCHAR,date VARCHAR);");
//Generate list View from ArrayList
displayListView();
checkButtonClick();
}
private void displayListView() {
ArrayList<Donor> donorList = new ArrayList<Donor>();
Donor donor;
Cursor c=db.rawQuery("SELECT name,bgroup FROM donors", null);
while(c.moveToNext())
{
donor = new Donor(c.getString(1),c.getString(0),false);
donorList.add(donor);
}
//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this,R.layout.list_markblood, donorList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
markblood.this.dataAdapter.getFilter().filter(cs);
System.out.print(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
markblood.this.dataAdapter.getFilter().filter(inputSearch.getText());
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Donor donor = (Donor) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), "Clicked on Row: " + donor.getName(), Toast.LENGTH_LONG).show();
}
});
}
private class MyCustomAdapter extends ArrayAdapter<Donor> {
private ArrayList<Donor> donorList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Donor>donorList) {
super(context, textViewResourceId, donorList);
this.donorList = new ArrayList<Donor>();
this.donorList.addAll(donorList);
}
private class ViewHolder {
TextView code;
CheckBox name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_markblood, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
Donor donor = (Donor) cb.getTag();
Toast.makeText(getApplicationContext(),
"Clicked on Checkbox: " + cb.getText() +
" is " + cb.isChecked(),
Toast.LENGTH_LONG).show();
donor.setSelected(cb.isChecked());
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
Donor donor = donorList.get(position);
holder.code.setText(" (" + donor.getCode() + ")");
holder.name.setText(donor.getName());
holder.name.setChecked(donor.isSelected());
holder.name.setTag(donor);
return convertView;
}
}
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
ArrayList<Donor> countryList = dataAdapter.donorList;
for(int i=0;i<countryList.size();i++){
Donor country = countryList.get(i);
if(country.isSelected()){
responseText.append("\n" + country.getName());
}
}
Toast.makeText(getApplicationContext(),
responseText, Toast.LENGTH_LONG).show();
}
});
}
}
答案 0 :(得分:0)
我认为您可以在操作栏中添加搜索功能。
在这里,您可以找到一个教程:http://www.edumobile.org/android/android-development/action-bar-search-view/