Unable to see Search Box (EditText) when using ListView in Android -
Unable to see Search Box (EditText) when using ListView in Android -
i'm trying implement custom listview (i.e. listview custom adapter) search box, have written next xml code--
<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="match_parent" android:orientation="vertical" tools:context=".customlistviewandroidexample" > <edittext android:id="@+id/txtinputsearch" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="search item.." android:inputtype="textvisiblepassword" /> <listview android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margintop="20dp" android:layout_weight="1" /> </linearlayout>
my menulistactivity code is---
public class menulistactivity extends activity { listview list; textview txtinputsearch; menulistadapter adapter; public menulistactivity customlistview = null; public arraylist<listmodel> customlistviewvaluesarr = new arraylist<listmodel>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_menu_list); customlistview = this; setlistdata(); resources res =getresources(); txtinputsearch=(textview)findviewbyid(r.id.txtinputsearch); list= ( listview )findviewbyid( r.id.list ); // list defined in xml ( see below ) adapter=new menulistadapter( customlistview, customlistviewvaluesarr,res ); list.setadapter( adapter ); txtinputsearch.addtextchangedlistener(new textwatcher() { @override public void aftertextchanged(editable arg0) { // todo auto-generated method stub string text = txtinputsearch.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 } }); } public void setlistdata() { (int = 0; < 11; i++) { final listmodel objlistmodel = new listmodel(); objlistmodel.setmenu("company "+i); objlistmodel.setprice("com.androidexample.customlistview:drawable/rihanna"); // sched.seturl("http:\\www."+i+".com"); customlistviewvaluesarr.add(objlistmodel); } } public void onitemclick(int mposition) { listmodel tempvalues = ( listmodel ) customlistviewvaluesarr.get(mposition); // show alert toast.maketext(customlistview,"clicked",toast.length_long).show(); } }
see adapter class below--
public class menulistadapter extends baseadapter implements onclicklistener { private activity activity; private arraylist data; private arraylist<listmodel> arraylist; private static layoutinflater inflater=null; public resources res; listmodel tempvalues=null; int i=0; public menulistadapter(activity paramactivity, arraylist paramdatalist, resources paramresource) { activity = paramactivity; data=paramdatalist; res = paramresource; arraylist=new arraylist<listmodel>(); arraylist.addall(data); inflater = ( layoutinflater )activity.getsystemservice(context.layout_inflater_service); } public int getcount() { if(data.size()<=0) homecoming 1; homecoming data.size(); } public object getitem(int position) { homecoming position; } public long getitemid(int position) { homecoming position; } public static class viewholder { public textview item_name; public textview item_price; } public view getview(int position, view convertview, viewgroup parent) { view vi = convertview; viewholder holder; if(convertview==null) { vi = inflater.inflate(r.layout.menu_list_row, null); holder = new viewholder(); holder.item_name = (textview) vi.findviewbyid(r.id.item_name); holder.item_price=(textview)vi.findviewbyid(r.id.item_price); // holder.image=(imageview)vi.findviewbyid(r.id.list_image); vi.settag( holder ); } else holder=(viewholder)vi.gettag(); if(data.size()<=0) { holder.item_name.settext("no data"); } else { tempvalues=null; tempvalues = ( listmodel ) data.get( position ); holder.item_name.settext( tempvalues.getmenu() ); holder.item_price.settext( tempvalues.getprice()); /* holder.image.setimageresource( res.getidentifier( "com.androidexample.customlistview:drawable/"+tempvalues.getimage() ,null,null));*/ vi.setonclicklistener(new onitemclicklistener( position )); } homecoming vi; } public void onclick(view v) { log.v("customadapter", "=====row button clicked====="); } private class onitemclicklistener implements onclicklistener { private int mposition; onitemclicklistener(int position) { mposition = position; } public void onclick(view arg0) { menulistactivity sct = (menulistactivity)activity; sct.onitemclick(mposition); } } // filter class public void filter(string chartext) { chartext = chartext.tolowercase(locale.getdefault()); data.clear(); if (chartext.length() == 0) { data.addall(arraylist); } else { (listmodel lm : arraylist) { if (lm.getmenu().tolowercase(locale.getdefault()).contains(chartext)) { data.add(lm); } } } notifydatasetchanged(); } }
in design mode, when clicks on graphical layout showing me proper/expected view (i.e. search box top , listview below search box) when run application showing listview , searchbox (edittext) invisible. please help.
change listview layout height android:layout_height="0dp"
instead of android:layout_height="fill_parent"
<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="match_parent" android:orientation="vertical" tools:context=".customlistviewandroidexample" > <edittext android:id="@+id/txtinputsearch" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="search item.." android:inputtype="textvisiblepassword" /> <listview android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_margintop="20dp" android:layout_weight="1" /> </linearlayout>
edit utilize relativelayout
instead of linearlayout
as
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"> <edittext android:id="@+id/txtinputsearch" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:completionthreshold="1" android:singleline="true"/> <listview android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#000000" android:layout_weight="1" android:layout_below="@+id/txtinputsearch" android:drawselectorontop="false"/> </relativelayout>
edit
use edittext
in code instead of textview
,so alter
textview txtinputsearch;
to
edittext txtinputsearch;
and
txtinputsearch=(textview)findviewbyid(r.id.txtinputsearch);
to
txtinputsearch=(edittext )findviewbyid(r.id.txtinputsearch);
android listview android-listview
Comments
Post a Comment