0

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

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;

}

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

    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();
           }
          });
4

1 回答 1

3

您希望您的适配器实现Filterable。然后覆盖 getFilter 执行搜索

@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") 从活动中调用搜索。

这也将用搜索结果替换列表视图中的项目列表。

于 2012-10-26T15:59:58.153 回答