Wednesday, 21 September 2016

Search View With Custom Listview




custom_listview:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/llContainer"
    >
    <TextView
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:id="@+id/text"
        android:text="hello"
        android:layout_alignParentTop="true"
        android:layout_marginTop="19dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@+id/text2"
        android:layout_toStartOf="@+id/text2" />

    <TextView
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:id="@+id/text2"
        android:text="listview"

        android:layout_alignBaseline="@+id/text"
        android:layout_alignBottom="@+id/text"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_alignTop="@+id/text"
        android:layout_toLeftOf="@+id/text2"
        android:layout_toStartOf="@+id/text2"
        android:layout_marginRight="31dp"
        android:layout_marginEnd="31dp"
        android:id="@+id/imageView" />
</RelativeLayout>

activity_search__view:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_search__view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.kashifahmed.myapplication.Search_View">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:id="@+id/search_view"
        android:hint="Enter Tex"
        />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list_view"
        android:layout_below="@+id/search_view"
        />
</RelativeLayout>


Show_View or Main Activity:

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class Search_View extends AppCompatActivity {


    private LinearLayout llContainer;
    private EditText etSearch;
    private ListView lvProducts;

    private ArrayList<Product> mProductArrayList = new ArrayList<Product>();

    private ArrayList<String> name = new ArrayList<String>();
    private ArrayList<String> points = new ArrayList<String>();
    private ArrayList<String> imgarr = new ArrayList<String>();


    private MyAdapter adapter1;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search__view);

        initialize();



        // Add Text Change Listener to EditText
        etSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // Call back the Adapter with current character to Filter
                adapter1.getFilter().filter(s.toString());
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    private void initialize() {
        etSearch = (EditText) findViewById(R.id.search_view);
        lvProducts = (ListView)findViewById(R.id.list_view);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        mProductArrayList.add(new Product("sdasdas","432","http://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg"));
        mProductArrayList.add(new Product("asdasd", "234235","http://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg"));
        mProductArrayList.add(new Product("cdszcc", "345345","http://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg"));
        mProductArrayList.add(new Product("zxczcxzxc","345345","http://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg"));
        mProductArrayList.add(new Product("zxczcx", "435345","http://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg"));
        mProductArrayList.add(new Product("zxczxvx", "345345","http://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg"));
        mProductArrayList.add(new Product("zvxvzv", "3465345","http://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg"));
        mProductArrayList.add(new Product("zxczxcz","345345","http://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg"));
        mProductArrayList.add(new Product("xzczxasqw","345345","http://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg"));
        mProductArrayList.add(new Product("kashif", "435345","http://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg"));
        mProductArrayList.add(new Product("fajad", "456546","http://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg"));
        mProductArrayList.add(new Product("assa","345346","http://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg"));





        adapter1 = new MyAdapter(Search_View.this, mProductArrayList);
        lvProducts.setAdapter(adapter1);
    }

    // Adapter Class
    public class MyAdapter extends BaseAdapter implements Filterable {

        private ArrayList<Product> mOriginalValues; // Original Values
        private ArrayList<Product> mDisplayedValues;    // Values to be displayed
        LayoutInflater inflater;

        public MyAdapter(Context context, ArrayList<Product> mProductArrayList) {
            this.mOriginalValues = mProductArrayList;
            this.mDisplayedValues = mProductArrayList;
            inflater = LayoutInflater.from(context);
        }

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

        @Override
        public Object getItem(int position) {
            return position;
        }

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

        private class ViewHolder {
            LinearLayout llContainer;
            TextView tvName,tvPrice;
            ImageView tvimg;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;

            if (convertView == null) {

                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.custom_listview, null);
                holder.tvName = (TextView) convertView.findViewById(R.id.text);
                holder.tvPrice = (TextView) convertView.findViewById(R.id.text2);
                holder.tvimg= (ImageView) convertView.findViewById(R.id.imageView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvName.setText(mDisplayedValues.get(position).name);
            holder.tvPrice.setText(mDisplayedValues.get(position).pointd+"");

            Picasso.with(Search_View.this).load(mDisplayedValues.get(position).imag).into(holder.tvimg);

            return convertView;
        }

        @Override
        public Filter getFilter() {
            Filter filter = new Filter() {

                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint,Filter.FilterResults results) {

                    mDisplayedValues = (ArrayList<Product>) results.values; // has the filtered values
                    notifyDataSetChanged();  // notifies the data with new filtered values
                }

                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
                    ArrayList<Product> FilteredArrList = new ArrayList<Product>();

                    if (mOriginalValues == null) {
                        mOriginalValues = new ArrayList<Product>(mDisplayedValues); // saves the original data in mOriginalValues
                    }

                    /********
                     *
                     *  If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
                     *  else does the Filtering and returns FilteredArrList(Filtered)
                     *
                     ********/
                    if (constraint == null || constraint.length() == 0) {

                        // set the Original result to return
                        results.count = mOriginalValues.size();
                        results.values = mOriginalValues;
                    } else {
                        constraint = constraint.toString().toLowerCase();
                        for (int i = 0; i < mOriginalValues.size(); i++) {
                            String data = mOriginalValues.get(i).name;
                            if (data.toLowerCase().startsWith(constraint.toString())) {
                                FilteredArrList.add(new Product(mOriginalValues.get(i).name,mOriginalValues.get(i).pointd,mOriginalValues.get(i).imag));
                            }
                        }
                        // set the Filtered result to return
                        results.count = FilteredArrList.size();
                        results.values = FilteredArrList;
                    }
                    return results;
                }
            };
            return filter;
        }
    }
}




Product Class 

import java.util.ArrayList;

/**
 * Created by Kashif Ahmed on 9/21/2016.
 */

public class Product {

    public String name;
    public String pointd;
    public String imag;

    public Product(String name, String points,String imag) {
        super();
        this.name = name;
        this.pointd = points;
        this.imag = imag;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPointd() {
        return pointd;
    }

    public void setPointd(String pointd) {
        this.pointd = pointd;
    }

    public String getImag() {
        return imag;
    }

    public void setImag(String imag) {
        this.imag = imag;
    }
}

No comments:

Post a Comment