android自定义检索,android-在自定义listview上实现搜索

我有一个listview和EditText字段,我想实现搜索,但是我的listview是一个自定义listview,其中填充了Item类的对象数组.当我按下键时,整个listview消失了,所以我想问题是它无法将char与类Item的对象匹配,这就是为什么当我按下键时没有任何结果.

这是我的代码

filterText = (EditText) findViewById(R.id.editTextfilter);

filterText.addTextChangedListener(filterTextWatcher);

m_items = new ArrayList();

phoneList=(ListView)findViewById(android.R.id.list);

phoneList.setTextFilterEnabled(true);

this.m_adapter = new ItemAdapter(this,R.layout.row,m_items);

phoneList.setAdapter(this.m_adapter);

private TextWatcher filterTextWatcher = new TextWatcher() {

public void afterTextChanged(Editable s) {

}

public void beforeTextChanged(CharSequence s, int start, int count,

int after) {

}

public void onTextChanged(CharSequence s, int start, int before,

int count) {

m_adapter.getFilter().filter(s.toString());

}

};

@Override

protected void onDestroy() {

super.onDestroy();

filterText.removeTextChangedListener(filterTextWatcher);

}

任何帮助如何使其有效

解决方法:

我有类似的要求.我使用了自定义过滤器.

ArrayList mPostingData = null;//arraylist of class items

ArrayList mTemp = new ArrayList();//temporary arraylist of class items

ArrayList mOri = new ArrayList();//Original arraylist

getListView().setTextFilterEnabled(true);

search= (EditText) findViewById(R.id.searchbox);

search.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence s, int start, int before, int count) {

mCustomListView.getFilter().filter(s);

mCustomListView.notifyDataSetChanged();

}

public void beforeTextChanged(CharSequence s, int start, int count,

int after) {

}

public void afterTextChanged(Editable s) {

}

});

POJO课程

class PostData {

String mID;

String mPostedBy;

String mTitle;

String mMessage;

String mImageUrl;

String mVideoUrl;

String mType ;

boolean me=false;

}

自定义列表视图,其中Filter方法被覆盖.基于POJO类的mTitle进行搜索

class CustomListView extends ArrayAdapter {

Context context;

LayoutInflater mInflater;

private Bitmap mIcon1;

private Bitmap mIcon2;

private Bitmap mIcon3;

PostData mp ;

public CustomListView(Context c)

{

super(c, 0);

mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

mIcon1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.text_icon);

mIcon2 = BitmapFactory.decodeResource(c.getResources(), R.drawable.image_icon);

mIcon3 = BitmapFactory.decodeResource(c.getResources(), R.drawable.video_icon);

}

public int getCount() {

if(mPostingData!=null){

return mPostingData.size();

}else{

return 0;

}

}

public void setData(ArrayList mPpst) {

mPostingData = mPpst;//contains class items data.

}

@Override

public Filter getFilter() {

return new Filter() {

@Override

protected void publishResults(CharSequence constraint, FilterResults results) {

if (results != null && results.count >= 0) {

setData((ArrayList) results.values);//if results of search is null set the searched results data

} else {

setData(mOri);// set original values

}

notifyDataSetInvalidated();

}

@Override

protected FilterResults performFiltering(CharSequence constraint) {

FilterResults result = new FilterResults();

if (!TextUtils.isEmpty(constraint)) {

constraint = constraint.toString().toLowerCase();

ArrayList foundItems = new ArrayList();

if(mTemp!=null)

{

for(int i=0;i

{

//If mTitle contains the string entered in Editext

if (mTemp.get(i).mTitle.toString().contains(constraint)) {

foundItems.add(mTemp.get(i));

}

else

{

}

}

}

result.count = foundItems.size();//search results found return count

result.values = foundItems;// return values

}

else

{

result.count=-1;// no search results found

}

return result;

}

};

}

public Object getItem(int arg0) {

// TODO Auto-generated method stub

return arg0;

}

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

///int type = getItemViewType(arg0);

Log.i("Aru","get View");

if(mPostingData == null ){

return null;

}

if (convertView == null) {

convertView = mInflater.inflate(R.layout.listviewimg, null);

convertView.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.FILL_PARENT,

LayoutParams.WRAP_CONTENT));

// Creates a ViewHolder and store references to the two children views

// we want to bind data to.

holder = new ViewHolder();

holder.ll=(LinearLayout) convertView.findViewById(R.id.lvid);

holder.text = (TextView) convertView.findViewById(R.id.texttitle);

holder.text2 = (TextView) convertView.findViewById(R.id.tvst);

holder.icon = (ImageView) convertView.findViewById(R.id.llimage);

convertView.setTag(holder);

} else {

holder = (ViewHolder) convertView.getTag();

}

mp = mPostingData.get(position);

String title = mp.mType;

if(mp.mTitle!=null && Name.equals(mp.mPostedBy )){

title = mp.mTitle+" "+title;

//holder.text.setBackgroundColor(Color.WHITE);

holder.ll.setBackgroundResource(R.drawable.listbkgme);

holder.text.setText(title);

}

else if(mp.mTitle!=null && Name!=mp.mPostedBy)

{

title = mp.mTitle+" "+title;

holder.text.setText(title);

}

if(mp.mMessage!=null && Name.equals(mp.mPostedBy )){

holder.ll.setBackgroundResource(R.drawable.listbkgme);

holder.text2.setText(mp.mMessage);

}

else if(mp.mMessage!=null && Name!=(mp.mPostedBy))

{

holder.text2.setText(mp.mMessage);

}

if(mp.mImageUrl!=null ){

holder.icon.setImageBitmap(mIcon2);

}else if(mp.mVideoUrl!=null){

holder.icon.setImageBitmap(mIcon3);

}else{

holder.icon.setImageBitmap(mIcon1);

}

return convertView;

}

class ViewHolder {

TextView text;

TextView text2;

ImageView icon;

LinearLayout ll;

}

public long getItemId(int position) {

return position;

}

}

根据您的要求修改以上内容.我已经测试了代码,并且可以正常工作.

标签:search,listview,android

来源: https://codeday.me/bug/20191208/2090215.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值