我尝试使用Search Box实现自定义ListView(即使用自定义适配器的ListView),因为我已经编写了以下XML代码 -
<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>
我的MenuListActivity代码是---
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();
}
}
请参阅我的适配器类 -
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();
}
}
在 设计模式 中,当我点击 图形布局 时,它会向我显示正确/预期的视图(即搜索框到顶部和ListView下面到搜索框)但是当我运行我的应用程序然后它只显示ListView和SearchBox(EditText)是不可见的。请帮助。
答案 0 :(得分:1)
将列表视图布局高度更改为android:layout_height="0dp"
而不是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>
修改使用RelativeLayout
代替LinearLayout
作为
<?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>
修改强>
在代码中使用EditText
而不是TextView
,因此请更改
TextView txtInputSearch;
到
EditText txtInputSearch;
并且
txtInputSearch=(TextView)findViewById(R.id.txtInputSearch);
到
txtInputSearch=(EditText )findViewById(R.id.txtInputSearch);
答案 1 :(得分:0)
问题是你在ListView中添加了一个权重,它会占用你布局的所有空间,而不是只是简单地包裹它的高度并删除android:layout_weight="1"
<强>样品:强>
<?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>
<强>问题:强>
txtInputSearch=(TextView)findViewById(R.id.txtInputSearch);
您将其投放到TextView
而不是EditText
。
将其更改为:
EditText txtInputSearch;
txtInputSearch=(EditText)findViewById(R.id.txtInputSearch);