Android中如何根据POJO类的类项在CustomListView中实现搜索? [英] How to implement search in CustomListView based on class item of POJO class in Android?

查看:17
本文介绍了Android中如何根据POJO类的类项在CustomListView中实现搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义列表视图.我必须显示来自网络服务器的数据.我需要根据来自 EditText 的输入实现搜索.ListView 中的每一行都包含图像、标题和消息.基于 WebServer 响应的图像更改.看看代码.

I have a Custom Listview. I have to display data from the webserver. I need to implement search based on input from EditText. Each row in ListView Contains Image, Title and Message. Image Changes based on response from WebServer. Have a Look at the code.

class CustomListView extends ArrayAdapter {

Context context;
LayoutInflater mInflater;
ArrayList<PostData> mPostingData = null;
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() {
    // TODO Auto-generated method stub
    if(mPostingData!=null){
        return mPostingData.size();
    }else{
        return 0;
    }
}

 public void setData(ArrayList<PostData> mPpst) {

    mPostingData = mPpst;
}


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) {
    // TODO Auto-generated method stub
    return position;
}

}

发布数据类

 class PostData {
String mID; 
String mPostedBy;
String mTitle;
String mMessage;
String mImageUrl;
String mVideoUrl;
String mType ;
boolean me=false;

}

根据标题排序.如何根据标题搜索列表视图?

Sorting Based on Title. How can i search the listview based on Title??

    search= (EditText) findViewById(R.id.searchbox);
     search.addTextChangedListener(new TextWatcher() {

         public void onTextChanged(CharSequence s, int start, int before, int count) {
             final String searchString=search.getText().toString();
               int textLength=searchString.length();



               Collections.sort(
                       mTempPost, 
                       new Comparator<PostData>() 
                       {



                        public int compare(PostData lhs, PostData rhs) {

                             System.out.println("........................"+lhs.mTitle+" "+rhs.mTitle);

                              return lhs.mTitle.compareTo(rhs.mTitle);
                        }


                       }
                     );

                         mCustomListView.notifyDataSetChanged();

         }

         public void beforeTextChanged(CharSequence s, int start, int count,
             int after) {


           }

           public void afterTextChanged(Editable s) {

              // mCustomListView.notifyDataSetChanged();
           }
          });

推荐答案

您希望您的适配器实现 可过滤.然后覆盖 getFilter 以执行搜索

You want your adapter to implement Filterable. Then override getFilter to perform the search

@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results != null && results.count >= 0) {
                setData(results.values);
            } else {
                setData(mPostingData);
            }

            notifyDataSetInvalidated();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults result = new FilterResults();
            if (!TextUtils.isEmpty(constraint)) {
                constraint = constraint.toString().toLowerCase();
                ArrayList<PostData> currentItems = new ArrayList<PostData>();
                ArrayList<PostData> foundItems = new ArrayList<PostData>();

                currentItems.addAll(mPostingData);

                for (PostData post: currentItems){
                    // Search for the items here. If we get a match, add to the list
                    if (post.mType.toLowerCase().contains(constraint)) {
                        foundItems.add(m);
                    } else if .... {
                    }
                }

                result.count = foundItems.size();
                result.values = foundItems;
            } else {
                result.count = -1;
            }

            return result;
        }
    };
}

然后,您将使用 adapter.getFilter().filter("Search Query") 从 Activity 中调用搜索.

You would then call the search from the activity with adapter.getFilter().filter("Search Query").

这也会用搜索结果替换您的列表视图中的项目列表.

This will replace the list of items in your listview with the search results too.

这篇关于Android中如何根据POJO类的类项在CustomListView中实现搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆