2016-01-04 71 views
0

小部件之間切換,我得到一個奇怪的動作條的行爲時,鬆動的功能。在我的動作條我有一個TextView,這增加了的話到ListView,一個刪除按鈕,即刪除一切與搜索查看,可以過濾我的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> 

回答

0

做了我的代碼重構,設置所有的變量是類變量,也使他們的私人(但它不是那麼重要,只是一個很好的做法)。現在一切正常。

+0

嗨@javastarter,你應該接受你自己的答案,因爲它解決您的問題。做得好! ;) – Fllo