我已经尝试了几个小时查看示例和其他人的代码,但我仍然不太理解它。
我有自己的方法,其中一半有效。
public class List_of_recipes extends ActionBarActivity {
//original
public List<List_view_class> list_view_class;
ArrayAdapter<List_view_class> adapter;
// not the original
public List<List_view_class> another_list_class;
public ListView list;
public EditText edit_text;
//ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_recipes);
// actionBar = getActionBar();
//actionBar.setDisplayHomeAsUpEnabled(true);
//actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#A02727")));
list = (ListView) findViewById(R.id.listView1);
edit_text = (EditText) findViewById(R.id.editText1);
list_view_class = new ArrayList<List_view_class>();
another_list_class = new ArrayList<List_view_class>();
adapter = new MyListAdapter(list_view_class);
list_view_class.add(new List_view_class(R.drawable.chicken, "Chicken salad", "15 min", "Easy", "4 serves"));
list_view_class.add(new List_view_class(R.drawable.mexican, "Mexican egg soup", "20 min", "Easy", "2 serves"));
list_view_class.add(new List_view_class(R.drawable.oyster, "Oysters with pancetta", "10 min", "Super Easy", "2 serves"));
//list_view_class.add(new List_view_class(R.drawable.wild_mushroom,"Wild mushroom crostini","5 min", "Easy","6 serves"));
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View viewclicked, int position, long id) {
// TODO Auto-generated method stub
if (position == 0) {
Intent i = new Intent(List_of_recipes.this, Chicken_salad.class);
startActivity(i);
finish();
}
if (position == 1) {
Intent i = new Intent(List_of_recipes.this, Mexican_egg.class);
startActivity(i);
finish();
}
if (position == 2) {
Intent i = new Intent(List_of_recipes.this, Oyster.class);
startActivity(i);
finish();
}
}
});
edit_text.addTextChangedListener(new TextWatcher() {
@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
String searchString = edit_text.getText().toString();
another_list_class.clear();
int textLength = edit_text.length();
for (int i = 0; i < list_view_class.size(); i++) {
String name = list_view_class.get(i).gettitle().toString();
if (textLength <= name.length()) {
if (searchString.equalsIgnoreCase(name.substring(0, textLength))) {
another_list_class.add(list_view_class.get(i));
}
}
}
adapter.notifyDataSetChanged();
adapter = new MyListAdapter(another_list_class);
list.setAdapter(adapter);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
});
}
public class MyListAdapter extends ArrayAdapter<List_view_class> {
public MyListAdapter(List<List_view_class> object_class) {
super(List_of_recipes.this, R.layout.activity_list_class_view, object_class);
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) List_of_recipes.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.activity_list_class_view, null, true);
}
List_view_class listclass = list_view_class.get(position);
ImageView image = (ImageView) rowView.findViewById(R.id.imageView1);
image.setImageResource(listclass.getrecipe_icon());
TextView date = (TextView) rowView.findViewById(R.id.textView1);
date.setText(listclass.gettitle());
TextView cook = (TextView) rowView.findViewById(R.id.cooktime);
cook.setText(listclass.getcook_time());
TextView diff = (TextView) rowView.findViewById(R.id.difficultylvl);
diff.setText(listclass.getdifficulty());
TextView serve = (TextView) rowView.findViewById(R.id.serving);
serve.setText(listclass.getmakes());
return rowView;
}
}
}
它没有给出任何错误,但无论我输入什么,我得到的只是第一个 ListView 项。
我不知道还要添加什么或者我在这里做错了什么。任何帮助将不胜感激,谢谢!
最佳答案
there is no need to create another adapter again and again on edit text change.you can refresh list view by just calling notifyDataSetChanged on adapter.
String searchString = s.toString();
another_list_class.clear();
for(int i =0; i< list_view_class.size(); i++){
String name = list_view_class.get(i).gettitle().toString();
if(name.contains(searchString))
{
another_list_class.add(list_view_class.get(i));
}
}
adapter.notifyDataSetChanged();
equalsIgnoreCase returns true if and only if two strings are exactly equal(Ignoring case sensitive);
You can also use startWith in place of contains
One more thing set your adpater with another_list_class arraylist initially.
because in this example we are changing search result in another_list_class .
最好将代码放在 afterTextChanged 中。
关于java - 如何使用编辑文本过滤自定义 ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33567970/