如何在android的工具栏中为此代码创建Filter Spinner的搜索栏

时间:2017-03-15 11:43:41

标签: java android

我已经在工具栏中创建了一个简单的微调器,并在微调器后面创建了一个搜索按钮,如ToolBar所有我需要的单击搜索按钮用编辑文本替换微调器我可以搜索项目顺便说一下,如果我点击旋转器中的项目,它会显示另一个项目来自数据库 这是我的xml,它有工具栏

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="abtech.waiteriano.com.waitrer.MenuActivity">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize" />
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:prompt="@string/ru_prompt" />
    <!--   <FrameLayout
       android:id="@+id/content_frame"
       android:layout_width="matc
        h_parent"
       android:layout_height="wrap_content"></FrameLayout>-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:id="@+id/rl1"
        android:orientation="horizontal"
        android:weightSum="2">
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/bottom_panel">
        </FrameLayout>

        <LinearLayout
            android:id="@+id/bottom_panel"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:weightSum="2"
            android:layout_alignParentBottom="true">
            <RelativeLayout
                android:id="@+id/fireID"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="#ff6b6b"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true">
                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:src="@drawable/fire" />
            </RelativeLayout>
            <RelativeLayout
                android:id="@+id/cartID"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="#6bfff3"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true">
                <ImageView
                    android:id="@+id/imageCart"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:src="@drawable/shoppingcart" />
            </RelativeLayout>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

这是具有工具栏组件的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/searchicon"
        android:icon="@drawable/search"
        android:title="@string/search"
        app:showAsAction="always"/>

    <item android:id="@+id/listicon"
        android:icon="@drawable/list_view"
        android:title="@string/listview"
        app:showAsAction="always"/>

    <item android:id="@+id/gridicon"
        android:icon="@drawable/table_grid"
        android:title="@string/gridview"
        app:showAsAction="always"/>
</menu>

这是工具bar.xml的活动

package abtech.waiteriano.com.waitrer;

import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.SortedMap;

import abtech.waiteriano.com.waitrer.connection_class.ConnectionClass;
import abtech.waiteriano.com.waitrer.fragments.LVMenuFragment;
import abtech.waiteriano.com.waitrer.fragments.MenuGridFragment;
import abtech.waiteriano.com.waitrer.fragments.TablesFragment;

public class MenuActivity extends AppCompatActivity {
    private android.support.v7.widget.Toolbar toolbar;
    private ArrayList<String> Prests_ID;
    private ArrayList<String> Prests_Name;
    public static String Prst_ID = "0";
    public static long Check_ID = 0;
    public static int Check_Serial = 0;
    ImageView listIcon, gridIcon;
    boolean flag_item_grid = true;
    RelativeLayout fire, showOrderCart;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        Prests_ID = new ArrayList<String>();
        Prests_Name = new ArrayList<String>();
        Prests_ID.add("-1");
        Prests_Name.add("ALL");
        Prst_ID = ConnectionClass.Ret_Col("Select MainCourse From OutLet_Setup Where OutLet_ID = " + ConnectionClass.OutletID);
        if (Prst_ID.trim() == "")
            Prst_ID = "0";

        String str = "Select ID,Code,Name,Name2,(Select Count(Preset_ID) AS Cont From MenuItems Where MenuItems.Preset_ID = Presets.ID And MenuItems.Rest_ID_Active = 1 AND MenuItems.OutLet_ID_Active = 1 AND MenuItems.Item_ID <> 0) AS Items_Count From Presets Where Active = 1 And Rest_ID_Active = 1 AND OutLet_ID_Active = 1 ORDER BY Code";
        ResultSet rs = ConnectionClass.Ret_RS(str);
        try {
            while (rs.next()) {
                Prests_ID.add(rs.getString("ID"));
                Prests_Name.add(rs.getString("Name") + " (" + rs.getString("Items_Count") + ")");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        int indx = -1;
        indx = Prests_ID.indexOf(Prst_ID);

        Spinner menu_spinner = (Spinner) findViewById(R.id.spinner);
        menu_spinner.setAdapter(new MyCustomAdapter(MenuActivity.this,
                R.layout.spinner_row, Prests_Name));
        menu_spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
        if (indx != -1)
            menu_spinner.setSelection(indx);
        listIcon = (ImageView) findViewById(R.id.listicon);
        gridIcon = (ImageView) findViewById(R.id.gridicon);
        fire = (RelativeLayout) findViewById(R.id.fireID);
        showOrderCart = (RelativeLayout) findViewById(R.id.cartID);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Prests_Name);
        navigationSpinner.setAdapter(adapter);
        if (toolbar.getParent() != null)
            ((ViewGroup) toolbar.getParent()).removeView(menu_spinner);
        toolbar.addView(menu_spinner, 0);

        fire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = 0;
                for (String key : TablesFragment.Check_Items.keySet()) {
                    SortedMap<String, String> sm1 = TablesFragment.Check_Items.get(key);
                    int qty = Integer.parseInt(sm1.get("Qty"));
                    if (qty > 0) {
                        i++;
                        double Price = Double.parseDouble(sm1.get("Price"));
                        Price = qty * Price;
                        String st = "insert into ChecksItems (Check_ID,Item_ID,QTY,UnitPrice,TotalPrice,DicountValue,Tax_Value,Adj_Value,NetPrice,Serial,Fired,Fired_Time,Voided,Voided_Time,Voided_Reason,P_On_Check,Complement,Status,IsModifier,Ref_Mod_Item,IsAssimbly,Ref_Ass_Item,Taxable,NoServiceCharge,Num_Fired,Num_Print,Server_ID,P_On_Report,Check_ID_Combine,Round_Check_Fired,Void_Effect_Invn,Promo_ID,Orig_Price,Officer,Comp_Reason_ID,End_Serial_Count,Discount_ID,Disc_Reason_ID,Hold,Hold_Time,Voided_By,Comp_By,Disc_By) values (" + String.valueOf(MenuActivity.Check_ID) + "," + sm1.get("Item_ID").toString() + ",1," + sm1.get("Price").toString() + "," + Price + ",0,0,0," + Price + "," + String.valueOf(i) + ",0,GetDate(),0,GetDate(),0,'" + sm1.get("PrintOnChick").toString() + "',0,'New',0,0,0,0,'" + sm1.get("Taxable").toString() + "','" + sm1.get("NoServiceCharge").toString() + "',0,0,0,'" + sm1.get("PrintOnReport").toString() + "',0,1,0,0," + sm1.get("Price").toString() + ",0,0,0,0,0,0,GetDate(),0,0,0)";
                        ConnectionClass.executeUpdate(st);
                    }
                }
                Toast.makeText(MenuActivity.this, "Fired", Toast.LENGTH_SHORT).show();
            }
        });
        showOrderCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MenuActivity.this, ShowOrderActivity.class);
                startActivity(i);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.iconmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(android.view.MenuItem item) {
        switch (item.getItemId()) {
            case R.id.searchicon:
                // ...
                return true;
            case R.id.listicon:
                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.content_frame, new LVMenuFragment()).commit();
                flag_item_grid = false;
                return true;
            case R.id.gridicon:
                // ...
                fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.content_frame, new MenuGridFragment()).commit();
                flag_item_grid = true;
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public class MyCustomAdapter extends ArrayAdapter<String> {

        public MyCustomAdapter(Context context, int textViewResourceId,
                               ArrayList<String> objects) {
            super(context, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
        }

        @Override
        public View getDropDownView(int position, View convertView,
                                    ViewGroup parent) {
            // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView,
                                  ViewGroup parent) {
            // TODO Auto-generated method stub
            // return super.getView(position, convertView, parent);

            LayoutInflater inflater = getLayoutInflater();
            View row = inflater.inflate(R.layout.spinner_row, parent, false);
            TextView label = (TextView) row.findViewById(R.id.menu_items);
            label.setText(Prests_Name.get(position));
            return row;
        }
    }

    public class MyOnItemSelectedListener implements AdapterView.OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent, View view, int pos,
                                   long id) {
            Prst_ID = Prests_ID.get(pos);
            Toast.makeText(MenuActivity.this,
                    "you selected: " + Prests_ID.get(pos) + " " + Prests_Name.get(pos),
                    Toast.LENGTH_SHORT).show();
            if (flag_item_grid) {
                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.content_frame, new MenuGridFragment()).commit();
            } else {
                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.content_frame, new LVMenuFragment()).commit();
            }
//            Toast.makeText(
//                    parent.getContext(),
//                    "The Item is "
//                            + parent.getItemAtPosition(pos).toString(),
//                    Toast.LENGTH_LONG).show();
        }

        public void onNothingSelected(AdapterView parent) {
            // Do nothing.
        }
    }

}

对不起,如果有什么东西是unClear,如果有任何遗漏,请发表评论

0 个答案:

没有答案