我试图在this教程后添加一个搜索ListView
的功能。问题是,当我在搜索EditText
中输入内容时,我无法显示任何内容。我希望能够显示用户正在搜索的内容,但这不是正在发生的事情。我需要对以下代码进行哪些更改才能实现?在Android中查询列表查看
代码:
private void loadList()
{
Cursor res = tbl_business_desc.getRegisterData();
int count = res.getCount();
lvListItemsData = new android.projects.helper.mylist.ListViewItems[count];
res.moveToFirst();
for (int i = 0; i < count; i++)
{
String ControlNum = res.getString(res.getColumnIndex(tbl_business_desc.col_ControlNum));
String BizName = res.getString(res.getColumnIndex(tbl_business_desc.col_BizName));
String Owner = res.getString(res.getColumnIndex(tbl_business_desc.col_Owner));
String Add = res.getString(res.getColumnIndex(tbl_business_desc.col_Add));
String Tel = res.getString(res.getColumnIndex(tbl_business_desc.col_Tel));
String Email = res.getString(res.getColumnIndex(tbl_business_desc.col_Email));
lvListItemsData[i] = new android.projects.helper.mylist.ListViewItems(ControlNum,BizName,Owner,Add,Tel,Email);
res.moveToNext();
}
lvList = (ListView) findViewById(R.id.lvMylist);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Set the adapter to our ListView
lvListadapter = new android.projects.helper.list.ListViewItemsAdapter(this, R.layout.listview_item_row_masterlist_body, lvListItemsData);
lvList.setAdapter(lvListadapter);
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MyListActivity.this.lvListadapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
lvList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
showStatus(position);
}
});
}
ListViewItemAdapter代码
public class ListViewItemsAdapter extends ArrayAdapter<ListViewItems> {
Context mContext;
int layoutResourceId;
ListViewItems data[] = null;
public ListViewItemsAdapter(Context mContext, int layoutResourceId, ListViewItems[] data) {
super(mContext, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItem = convertView;
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
listItem = inflater.inflate(layoutResourceId, parent, false);
// get the elements in the layout
TextView textControlNum = (TextView) listItem
.findViewById(R.id.txtControlNum);
TextView textBizName = (TextView) listItem
.findViewById(R.id.txtBizName);
TextView textOwner = (TextView) listItem
.findViewById(R.id.txtOwner);
TextView textAdd = (TextView) listItem
.findViewById(R.id.txtAdd);
TextView textTel = (TextView) listItem
.findViewById(R.id.txtTel);
TextView textEmail = (TextView) listItem
.findViewById(R.id.txtEmail);
ListViewItems listViewItems = data[position];
try {
textControlNum.setText(listViewItems.ControlNum);
textBizName.setText(listViewItems.BizName);
textOwner.setText(listViewItems.Owner);
textAdd.setText(listViewItems.Add);
textTel.setText(listViewItems.Tel);
textEmail.setText(listViewItems.Email);
}
catch (Exception e){}
if(position % 2 == 0){
listItem.setBackgroundColor(Color.rgb(243,251,254));
}else{
listItem.setBackgroundColor(Color.rgb(255,255,255));
}
return listItem;
}
}
在onCreate方法的活动中写入监听器代码 –
仍是同样的问题,在搜索文本中输入时没有结果。 – LadyWinter
你能显示'ListViewItemsAdapter'吗? – KeLiuyue