我已經嘗試了幾個小時查看示例和其他人的代碼,但我仍然不太明白它。如何使用編輯文本過濾自定義ListView?
我有我自己的方法,其中一半工作。
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;
}
}
}
它沒有給出任何錯誤,但是我得到的是第一個列表視圖項,不管我輸入什麼。
我不知道還有什麼要補充或我在做什麼錯在這裏。任何幫助將不勝感激,謝謝!
你是什麼意思「過濾器」與edittexts?你想建立自己的Listview項目嗎? – Paul
嘗試移動notifyDataSetChanged(),設置新的適配器後 – X3Btel