待办事项列表Android应用程序在搜索后崩溃

时间:2015-11-28 23:50:21

标签: android

我遇到的问题是,一旦我在ListView上执行搜索,我就无法删除或添加任何其他内容到我的列表中。此外,我收到IndexOutofBounds错误。我的代码如下:

public class MainActivity extends AppCompatActivity {

public ArrayList<String> array;
public ArrayAdapter<String> adapt;
public ListView list;
public EditText search;

@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list = (ListView) findViewById(R.id.list_item);
    array = new ArrayList<>();
    adapt = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, array);
    list.setAdapter(adapt);
    setupListViewListener();
    search();
}

public void onAddItem(View v) {
    EditText edit = (EditText) findViewById(R.id.edit);
    String item = edit.getText().toString();
    adapt.add(item);
    edit.setText("");
}

public void setupListViewListener() {
    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View v, int i, long l) {
            array.remove(i);
            adapt.notifyDataSetChanged();
            return true;
        }
    });
}

public void search()
{
    search = (EditText) findViewById(R.id.inputSearch);
    search.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            adapt.getFilter().filter(cs);
            adapt.notifyDataSetChanged();
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        @Override
        public void afterTextChanged(Editable arg0) {
        }
    });
}

public void deleteAll(View view) {
    adapt.clear();
    adapt.notifyDataSetChanged();
}

}

0 个答案:

没有答案