Adding search functionality to custom Listview

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Adding search functionality to custom Listview

Postby nickmoi451 » Mon Apr 08, 2013 4:28 am

Adding search functionality to listview will filters the list data with a matching string, hence provides user an easy way to find the information he needs. In this simple tutorial i am discussing how to enable search filter to android custom ListView.
In this tutorial,we will display list of picture and search picture by its name.

Filter Method in Adapter

To filter the list data with a matching string from user,we use a Filter method in Adapter.
Code: Select all

     /**
* Filter
* @author 9Android.net
*
*/
public void filter(String charText) {
    charText = charText.toLowerCase();
    picList.clear();
    if (charText.length() == 0) {
        picList.addAll(listpicOrigin);
    } else {
        for (Picture pic : listpicOrigin) {
            if (pic.getPicName().toLowerCase().contains(charText)) {
                picList.add(pic);
            }
        }
    }
    notifyDataSetChanged();
}

PicListAdapter.java
Hide content    

public class PicListAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<Picture> picList = null;
    private ArrayList<Picture> listpicOrigin;

    public PicListAdapter(Context context, List<Picture> picList) {
        mContext = context;
        this.picList = picList;
        mInflater = LayoutInflater.from(mContext);
        this.listpicOrigin = new ArrayList<Picture>();
        this.listpicOrigin.addAll(picList);
    }

    public class ViewHolder {
        TextView picName;
        TextView picType;
        ImageView picIcon;
    }

    public View getView(int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = mInflater.inflate(R.layout.list_item, null);
            holder.picName = (TextView) view.findViewById(R.id.pic_name_txt);
            holder.picType = (TextView) view.findViewById(R.id.pic_type_txt);
            holder.picIcon = (ImageView) view.findViewById(R.id.pic_icon_img);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.picName.setText(picList.get(position).getPicName());
        holder.picType.setText(picList.get(position).getPicType());
        holder.picIcon.setImageResource(picList.get(position).getPicSource());

        return view;
    }

    public int getCount() {
        return picList.size();
    }

    public Picture getItem(int position) {
        return picList.get(position);
    }

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

    /**
     * Filter
     * @author 9Android.net
     *
     */
    public void filter(String charText) {
        charText = charText.toLowerCase();
        picList.clear();
        if (charText.length() == 0) {
            picList.addAll(listpicOrigin);
        } else {
            for (Picture pic : listpicOrigin) {
                if (pic.getPicName().toLowerCase().contains(charText)) {
                    picList.add(pic);
                }
            }
        }
        notifyDataSetChanged();
    }

}



MainActivity.java

Code: Select all

public class MainActivity extends Activity implements TextWatcher,
        OnItemClickListener {

    private static final int LIST_PIC_SCREEN = 0;
    private static final int VIEW_PIC_SCREEN = 1;

    private String[] listPicName = { "androidgirl_1", "androidgirl_2",
            "androidgirl_3", "beautifulgirl_1", "beautifulgirl_2",
            "bikinigirl_1", "bikinigirl_2", "bikinigirl_3", "cutegirl_1",
            "cutegirl_2", "cutegirl_3", "cutegirl_4", "cutegirl_5",
            "hotgirl_1", "hotgirl_2", "hotgirl_3", "nudegirl_1", "nudegirl_2",
            "nudegirl_3", "nudegirl_4", "sexygirl_1", "sexygirl_2",
            "sexygirl_3", "sportgirl_1", "sportgirl_2", "sportgirl_3",
            "sportgirl_4" };

    private String[] listPicType = { "android", "android", "android",
            "beautiful", "beautiful", "bikini", "bikini", "bikini", "cutegirl",
            "cutegirl", "cutegirl", "cutegirl", "cutegirl", "hotgirl",
            "hotgirl", "hotgirl", "nude", "nude", "nude", "nude", "sexy",
            "sexy", "sexy", "sport", "sport", "sport", "sport" };
    private int[] listPicDrawable = { R.drawable.androidgirl_1,
            R.drawable.androidgirl_2, R.drawable.androidgirl_3,
            R.drawable.beautifulgirl_1, R.drawable.beautifulgirl_2,
            R.drawable.bikinigirl_1, R.drawable.bikinigirl_2,
            R.drawable.bikinigirl_3, R.drawable.cutegirl_1,
            R.drawable.cutegirl_2, R.drawable.cutegirl_3,
            R.drawable.cutegirl_4, R.drawable.cutegirl_5, R.drawable.hotgirl_1,
            R.drawable.hotgirl_2, R.drawable.hotgirl_3, R.drawable.nudegirl_1,
            R.drawable.nudegirl_2, R.drawable.nudegirl_3,
            R.drawable.nudegirl_4, R.drawable.sexygirl_1,
            R.drawable.sexygirl_2, R.drawable.sexygirl_3,
            R.drawable.sportgirl_1, R.drawable.sportgirl_2,
            R.drawable.sportgirl_3, R.drawable.sportgirl_4

    };

    private ArrayList<Picture> listPic = new ArrayList<Picture>();
    private ListView listview;
    private PicListAdapter adapter;
    private EditText searchEdt;
    private ViewFlipper fliper;
    private ImageView viewpic;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fliper = (ViewFlipper) findViewById(R.id.viewFlipper1);
        listview = (ListView) findViewById(R.id.listView1);
        for (int i = 0; i < listPicName.length; i++) {
            Picture pic = new Picture(listPicName[i], listPicType[i],
                    listPicDrawable[i]);
            listPic.add(pic);
        }
        adapter = new PicListAdapter(this, listPic);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(this);

        searchEdt = (EditText) findViewById(R.id.search_edt);
        searchEdt.addTextChangedListener(this);
        viewpic = (ImageView) findViewById(R.id.viewpic_img);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    /**
     * @author 9Android.net
     */
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub

    }

    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        String text = searchEdt.getText().toString().toLowerCase();
        adapter.filter(text);
    }

    /**
     *  @author 9Android.net
     */
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        Sliding.slideFromRightToLeft(VIEW_PIC_SCREEN, fliper);
        viewpic.setImageResource(listPic.get(position).getPicSource());

    }

    /**
     * @author 9Android.net
     * @param : event, keycode of downed button
     * @Objective : Handle keyevent on main activity
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            int screen = fliper.getDisplayedChild();

            if (screen == VIEW_PIC_SCREEN) {
                Sliding.slideFromLeftToRight(LIST_PIC_SCREEN, fliper);
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}


Donwload source project: http://www.dl.9android.net/index.php?ac ... 1362959837
Source: http://www.9android.net/add-search-func ... -listview/
nickmoi451
Freshman
Freshman
 
Posts: 6
Joined: Mon Apr 08, 2013 2:12 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests