Reputation: 157
I'm trying to implement Custom ListView (i.e. ListView with custom Adapter) with Search Box, for that I have written the following 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()
{
for (int i = 0; i < 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 my 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)
return 1;
return data.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return 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 ));
}
return 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
{
for (ListModel lm : arrayList)
{
if (lm.getMenu().toLowerCase(Locale.getDefault()).contains(charText))
{
data.add(lm);
}
}
}
notifyDataSetChanged();
}
}
In design mode, when I clicks on Graphical Layout It is showing me proper/expected view (i.e. Search box to the Top and ListView Below to the search Box) but when I run my application then it is showing only ListView and SearchBox (EditText) is invisible. Please Help.
Upvotes: 0
Views: 297
Reputation: 14408
Change listview layout height to 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 Use 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 your code instead of TextView
,So change
TextView txtInputSearch;
to
EditText txtInputSearch;
And
txtInputSearch=(TextView)findViewById(R.id.txtInputSearch);
to
txtInputSearch=(EditText )findViewById(R.id.txtInputSearch);
Upvotes: 1
Reputation: 26198
The problem is that you are adding a weight into you ListView which would take all the space of your layout, instead dont put any weight on it just simply wrap its height and remove the android:layout_weight="1"
sample:
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:layout_height="wrap_content"
android:hint="Search item.."
android:inputType="textVisiblePassword" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
</LinearLayout>
problem:
txtInputSearch=(TextView)findViewById(R.id.txtInputSearch);
You are casting it to a TextView
not an EditText
.
change it to this:
EditText txtInputSearch;
txtInputSearch=(EditText)findViewById(R.id.txtInputSearch);
Upvotes: 0