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