我需要在自定义适配器列表视图中的操作栏搜索视图

时间:2017-01-23 08:57:04

标签: android listview android-actionbar android-search

在我的应用程序中,有一个带有textview和image的listview,我可以通过在xml中添加一个edittext来搜索元素,但我想在操作栏中搜索栏..我该怎么做?请帮忙..     Image:

      // My activity java code

productName = new String[] { "Baby Item", "Harbal Item", "Medicine Item", "Electronics Item", "Product item 1",
        "Product item 2", "Product item 3", "Product item 4", "Product item 5", "Product item 6"};

productDescesc = new String[] { "Baby Item description", "Harbal Item description",
        "Medicine Item description", "Electronics Item description", "Product item description",
        "Product item description", "Product item description", "Product item description", "Product item description","Product item description" };

    productIcon = new int[] { R.drawable.baby_item, R.drawable.harbal_item,
            R.drawable.medicine_item, R.drawable.electronics_item,
            R.drawable.demo_product_item, R.drawable.demo_product_item, R.drawable.demo_product_item,
            R.drawable.demo_product_item, R.drawable.demo_product_item, R.drawable.demo_product_item };

    // Locate the ListView in listview_main.xml
    list = (ListView) findViewById(R.id.list);

    for (int i = 0; i < productName.length; i++)
    {
        SearchProduct searchProduct = new SearchProduct(productIcon[i], productName[i], productDescesc[i]);
        // Binds all strings into an array
        arraylist.add(searchProduct);
    }

    // Pass results to ListViewAdapter Class
    adapter = new ListViewAdapter(this, arraylist);

    // Binds the Adapter to the ListView
    list.setAdapter(adapter);

    // Locate the EditText in listview_main.xml
    editsearch = (EditText) findViewById(R.id.search);

    // Capture Text in EditText
    editsearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
            adapter.filter(text);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                                      int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {
            // TODO Auto-generated method stub
        }


        });

        //Onclick
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {

//                                Toast.makeText(Search.this,"List Item Clicked:" + position, Toast.LENGTH_SHORT)
//                        .show();
                Intent intent=new Intent(Search.this,ProductActivity.class);
                startActivity(intent);

            }
        });

//我的自定义适配器代码

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context mContext;
    LayoutInflater inflater;
    private List<SearchProduct> productList = null;
    private ArrayList<SearchProduct> arraylist;
    SearchProduct searchProduct;
    private ListViewAdapter filter;

    public ListViewAdapter(Context context,
                           List<SearchProduct> worldpopulationlist) {
        mContext = context;
        this.productList = worldpopulationlist;
        inflater = LayoutInflater.from(mContext);
        this.arraylist = new ArrayList<SearchProduct>();
        this.arraylist.addAll(worldpopulationlist);
    }

    public class ViewHolder {
        ImageView icon;
        TextView name;
        TextView desc;
    }

    @Override
    public int getCount() {
        return productList.size();
    }

    @Override
    public SearchProduct getItem(int position) {
        return productList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.search_list_item, null);

            // Locate the TextViews in listview_item.xml
            holder.icon = (ImageView) view.findViewById(R.id.imgProduct);
            holder.name = (TextView) view.findViewById(R.id.tvProductName);
            holder.desc = (TextView) view.findViewById(R.id.tvDesc);
            // Locate the ImageView in listview_item.xml

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // Set the results into TextViews
        holder.name.setText(productList.get(position).getName());
        holder.desc.setText(productList.get(position).getDesc());

        // Set the results into ImageView
        holder.icon.setImageResource(productList.get(position)
                .getIcon());

        return view;
    }

    // Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        productList.clear();
        if (charText.length() == 0) {
            productList.addAll(arraylist);
        } else {
            for (SearchProduct searchProduct : arraylist) {
                if (searchProduct.getName().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    productList.add(searchProduct);
                }
            }
        }
        notifyDataSetChanged();
    }

}

3 个答案:

答案 0 :(得分:0)

您不应在xml中添加EditText来执行此操作。 您想要做的是自定义工具栏。 此链接可能有所帮助:http://www.101apps.co.za/index.php/articles/using-toolbars-in-your-apps.html

因此,您的MainActivity扩展了您的CustomToolBarActivity,您现有的工作将处于片段中。

答案 1 :(得分:0)

<强> activity_main.xml中:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_search"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/blue">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_horizontal"
                android:orientation="horizontal">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:id="@+id/search_toolbar_id"
                android:hint="search_here"/>

            </LinearLayout>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize" />

</android.support.design.widget.CoordinatorLayout>

<强> MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = AppDelegate.getContext();
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_browse_and_search);
    setSupportActionBar(toolbar);

    EditText searchText = (EditText) findViewById(R.id.search_toolbar_id);
    // Put in another button(search icon) to do whatever you want with an OnClickListener

    // Put your existing work in a fragment and replace the frame layout with your fragment
    Fragment yourExistingWorkInThisFragment = new Fragment();
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.frame_layout, yourExistingWorkInThisFragment)
            .commit();

答案 2 :(得分:0)