我正在尝试在列表视图中动态添加电影名称,然后如果在列表视图中找不到电影名称,我想使用搜索选项来查找电影名称在列表视图中添加电影。我的问题是在使用搜索之前,插入了电影名称在列表视图中正确使用搜索后,电影名称未插入列表视图中。
public class MainActivity extends ListActivity implements OnClickListener {
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
EditText et,inputSearch;
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
et=(EditText) findViewById(R.id.editText);
final EditText inputSearch = (EditText) findViewById(R.id.inputSearch);
btn=(Button) findViewById(R.id.addBtn);
btn.setOnClickListener(this);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
MainActivity.this.adapter.getFilter().filter(cs);
adapter.notifyDataSetChanged();
}
@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
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (isInputValid(et)) {
listItems.add(0,et.getText().toString());
et.setText("");
adapter.notifyDataSetChanged();
}
}
protected boolean isInputValid(EditText etInput2) {
// TODO Auto-generatd method stub
if (etInput2.getText().toString().trim().length()<1) {
etInput2.setError("Please Enter Item");
return false;
} else {
return true;
}
}
}