Android搜索视图不起作用

时间:2017-04-20 10:05:46

标签: android

  

位于操作栏中的搜索栏。当我点击它时,没有任何操作。

Activity.java

 public class MainActivity extends AppCompatActivity {
    DrawerLayout mDrawerLayout;
    NavigationView mNavigationView;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;
    ImageView profileiamge;
    DatabaseReference mDatabase;
    private RecyclerView recyclerView1;
    SearchAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        /**
         *Setup the DrawerLayout and NavigationView
         */
        String username = getIntent().getStringExtra("username");
        String imageurl = getIntent().getStringExtra("imageurl");
        String email = getIntent().getStringExtra("email");
        Log.e("url",imageurl);

             mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
             mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
        View header = mNavigationView.getHeaderView(0);
        profileiamge=(ImageView)header.findViewById(R.id.imageView5) ;
        new DownloadImageTask(profileiamge).execute(imageurl);

        /**
         * Lets inflate the very first fragment
         * Here , we are inflating the TabFragment as the first Fragment
         */

             mFragmentManager = getSupportFragmentManager();
             mFragmentTransaction = mFragmentManager.beginTransaction();
             mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
        /**
         * Setup click events on the Navigation View Items.
         */

             mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
             @Override
             public boolean onNavigationItemSelected(MenuItem menuItem) {
                mDrawerLayout.closeDrawers();



                 if (menuItem.getItemId() == R.id.nav_item_sent) {
                     FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                     fragmentTransaction.replace(R.id.containerView,new SentFragment()).commit();

                 }

                if (menuItem.getItemId() == R.id.nav_item_inbox) {
                    FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                    xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
                }

                 return false;
            }

        });

        /**
         * Setup Drawer Toggle of the Toolbar
         */

                android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
                ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
                R.string.app_name);
                toolbar.setTitle(" ");
                mDrawerLayout.setDrawerListener(mDrawerToggle);

                mDrawerToggle.syncState();
        mDatabase = FirebaseDatabase.getInstance().getReference();
        GeoFire geoFire = new GeoFire(mDatabase.child("Location"));
        geoFire.setLocation("firebase-hq1", new GeoLocation(37.7855889, -122.4056973), new GeoFire.CompletionListener() {
            @Override
            public void onComplete(String key, DatabaseError error) {
                if (error != null) {
                    System.err.println("There was an error saving the location to GeoFire: " + error);
                } else {
                    System.out.println("Location saved on server successfully!");
                }
            }

        });


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
       // getMenuInflater().inflate(R.menu.main, menu);
        SearchManager searchManager = (SearchManager)
                getSystemService(Context.SEARCH_SERVICE);
      MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) searchMenuItem.getActionView();

        searchView.setSearchableInfo(searchManager.
                getSearchableInfo(getComponentName()));
        searchView.setSubmitButtonEnabled(true);
        SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(final String query) {
Log.e("Search", "Search");
                return false;
            }

            @Override
            public boolean onQueryTextChange(final String newText) {


                Log.e("Search", "Search");
                DatabaseReference databaseReference1 = mDatabase.child("Coupon");
                databaseReference1.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        List<Coupon> couponList = new ArrayList<>();
                        for (DataSnapshot val : dataSnapshot.getChildren()) {
                            //I am not sure what record are you specifically looking for
                            //This is if you are getting the Key which is the record ID for your Coupon Object
                            if (val.getKey().contains(newText)) {
                                // count++;
                                Coupon coupon = new Coupon();
                                coupon = dataSnapshot.child(val.getKey()).getValue(Coupon.class);
                                couponList.add(coupon);
                            }

                            //This is if your are querying for the hotel child
                            if (val.child("hotel").getValue(String.class).contains(newText)) {
                                //Do what you want with the record
                            }
                        }
                        recyclerView1 = (RecyclerView) findViewById(R.id.recycler_view);

                        //   prepareMovieData();
                        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(MainActivity.this);
                        recyclerView1.setLayoutManager(mLayoutManager);
                        recyclerView1.setItemAnimator(new DefaultItemAnimator());
                        mAdapter = new SearchAdapter(MainActivity.this, couponList);
                        recyclerView1.setAdapter(mAdapter);
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }

                });

                return true;
            }
        };
        searchView.setOnQueryTextListener(textChangeListener);
        return true;

    }
}

我把logs放在onQueryTextSubmit和onQueryTextChange方法上,但logcat中没有登录。这意味着搜索视图没有被触发。

main.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_search"
        android:orderInCategory="100"
        android:icon="@drawable/search"
        android:title="@string/action_settings"
        android:layout_centerInParent="true"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="android.widget.SearchView" />
    <item
        android:id="@+id/action_alarm"
        android:orderInCategory="100"
        android:icon="@drawable/alarm"
        android:title="@string/action_settings"
        app:showAsAction="ifRoom" />
</menu>

希望有人可以帮我解决这个问题。谢谢你。

1 个答案:

答案 0 :(得分:1)

请查看此YouTube链接Link 你需要这样的输出吗?