好吧,我解決了這個問題@邁克爾克勞斯的幫助。多謝兄弟!我複製了temp的另一個arraylist,並在textchangedlistener中使用。
public class ItemList extends Activity {
ListView lvItems;
EditText tbSearch;
//String[] arrMonth = {"January","February","March","April","May","June","July","August","September","October","November","December"};
ArrayList<String> arrMonth = new ArrayList<>();
ArrayList<String> temp = new ArrayList<>();
ArrayAdapter<String> aaItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
lvItems = (ListView) findViewById(R.id.lvItems);
tbSearch = (EditText) findViewById(R.id.tbSearch);
PopulateArrayList();
temp.addAll(arrMonth);
aaItems = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrMonth);
lvItems.setAdapter(aaItems);
tbSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String txt = tbSearch.getText().toString().toLowerCase();
aaItems.clear();
if (s.length() == 0) {
aaItems.addAll(temp);
} else {
for (String item : temp) {
if (item.toLowerCase().contains(txt)) {
aaItems.add(item);
}
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
public void btnAdd_Click(View v)
{
aaItems.add("Patrick");
}
public void PopulateArrayList()
{
arrMonth.add("January");
arrMonth.add("February");
arrMonth.add("March");
arrMonth.add("April");
arrMonth.add("May");
arrMonth.add("June");
arrMonth.add("July");
arrMonth.add("August");
arrMonth.add("September");
arrMonth.add("October");
arrMonth.add("November");
arrMonth.add("December");
}
}
您是否在適配器上通過了「notifyDataSetChanged」? – tachyonflux
看看這個答案 - http://stackoverflow.com/questions/5125350/android-arrayadapter-add-method-not-working – Zain
notifyDataSetChanged不適用於我。 –