在ActionBar中切换小部件时失去功能

时间:2016-01-04 21:14:45

标签: android

我得到了一个奇怪的ActionBar行为。在我的ActionBar中,我有一个TextView,它向ListView添加单词,删除按钮,删除所有内容,还有一个SearchView,用于过滤我的ListView。问题是,当我启动我的应用程序时,我可以添加单词,删除它们并使用SearchView。但在使用SearchView之后,我无法添加单词并删除它们,只有在我完全重启我的应用程序之后。请帮我解决这个问题,我花了很多时间但没有运气。

这是截图:

appscreen

这是我的代码:

    public class MainActivity extends AppCompatActivity {

    ListView listView;
    ArrayList<String> arraylist;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        arraylist = new ArrayList<String>();

        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arraylist);
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        /** Create an option menu from res/menu/items.xml */
        getMenuInflater().inflate(R.menu.main_menu, menu);
        /** Get the action view of the menu item whose id is search */
        View v = (View) menu.findItem(R.id.action_submit).getActionView();

        /** Get the edit text from the action view */
        final EditText txtSearch = (EditText) v.findViewById(R.id.editText);
        final Button button = (Button) v.findViewById(R.id.buttonSave);

        /** Setting an action listener */

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                arraylist.add(txtSearch.getText().toString());
                adapter.notifyDataSetChanged();
            }
        });

        MenuItem pinMenuItem = menu.findItem(R.id.delete_all);

        pinMenuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                arraylist.clear();
                adapter.notifyDataSetChanged();
                return true;
            }
        });


        MenuItem ourSearchItem = menu.findItem(R.id.menu_item_search);

        SearchView sv = (SearchView) ourSearchItem.getActionView();

        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                adapter.getFilter().filter(newText);
                if (Arrays.asList(arraylist).contains(newText)){
                    Toast.makeText(getApplicationContext(), "This word is in the list", Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }
}

main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_submit"
        app:actionLayout="@layout/add_layout"
        app:showAsAction="ifRoom|collapseActionView"
        android:title="Add"
        android:icon="@drawable/ic_action_content_add"/>

    <item
        android:id="@+id/delete_all"
        android:title="Delete all"
        app:showAsAction="always"
        android:icon="@drawable/ic_action_action_delete"
        />

    <item
        app:showAsAction="ifRoom|collapseActionView"
        android:icon="@drawable/ic_action_action_pageview"
        android:id="@+id/menu_item_search"
        app:actionViewClass="android.widget.SearchView"
        android:title="Search"
        android:iconifiedByDefault="true"/>

</menu>

add_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_toRightOf="@+id/buttonSave"
        android:layout_toEndOf="@+id/buttonSave" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:id="@+id/buttonSave"
        android:layout_alignBottom="@+id/editText" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

重构了我的代码,将所有变量设置为类变量并将它们设为私有(但它不是那么重要,只是一个好习惯)。现在一切正常。