Android ListView不会被Filter刷新

时间:2015-06-28 05:26:48

标签: android android-listview

在我的应用程序中,我有三个片段 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="horizontal"> <include android:id="@+id/app_bar" layout="@layout/app_bar" /> <fragment android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/fragment_buttons" class="com.example.user.timeline.buttonsFragment" tools:layout="@layout/fragment_buttons" android:layout_weight="1" android:layout_gravity="left"> </fragment> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" class="com.example.user.timeline.Fragment_1to6" android:id="@+id/fragment_1to6" tools:layout="@layout/fragment_buttons" android:layout_weight="3"> </FrameLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment_7to12" android:layout_weight="3"> </FrameLayout> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="match_parent" tools:context="com.example.user.timeline.buttonsFragment" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:text="ADD ACTIVITY" android:width="40dp" android:height="30dp" android:background="#B6B6B6" android:textColor="#000000" android:onClick="onClick" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ADD ACTIVITY" android:id="@+id/button2" android:width="40dp" android:height="30dp" android:background="#B6B6B6" android:textColor="#000000" android:onClick="onClick" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ADD ACTIVITY" android:id="@+id/button3" android:layout_column="0" android:width="40dp" android:height="30dp" android:background="#B6B6B6" android:textColor="#000000" android:onClick="onClick" /> </LinearLayout> 。其中一个片段我从手机联系人列表中获得了ViewPager Arraylist,并且我在输入ListView后试图对其进行过滤。 但是在edittext可见之前不会刷新,我必须通过过滤的字符串隐藏键盘以刷新列表视图。

例如:

过滤列表视图&#34; a&#34;:

softkeyboard

我的适配器:

adapter.getFilter().filter("a");

public class AdapterContacts extends BaseAdapter implements Filterable { private LayoutInflater inflater; private Context context; private List<ContactLists> categoryArrayList; private final ArrayList<ContactLists> originalList = new ArrayList<ContactLists>(); private NameFilter filter; public AdapterContacts(ArrayList<ContactLists> array) { categoryArrayList = array; } public AdapterContacts(Context context, List<ContactLists> array) { this.context = context; inflater = LayoutInflater.from(this.context); categoryArrayList = array; originalList.addAll(array); } @Override public int getCount() { return categoryArrayList.size(); } @Override public ContactLists getItem(int position) { return categoryArrayList.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder mViewHolder; if (convertView == null) { convertView = inflater.inflate(R.layout.layout_contacts_list_item, null); mViewHolder = new ViewHolder(convertView); convertView.setTag(mViewHolder); } else { mViewHolder = (ViewHolder) convertView.getTag(); } ContactLists item = getItem(position); mViewHolder.fillItems(this, item, position); return convertView; } private static class UI extends HelperUI { public TextView tv_person_nickname_mobile_number; public TextView btn_invite_message; public ImageView img_contact_image; public ImageView imgv_user_rank; public TextView tv_contact_name; public LinearLayout ll_root; public UI(View view) { parseUi(view); } } private class ViewHolder { private UI UI; public ViewHolder(View view) { UI = new UI(view); } public void fillItems(final AdapterContacts adapter, final ContactLists item, final int position) { UI.tv_contact_name.setText(item.getContact_name()); if (item.getStatus() == 1) { UI.btn_invite_message.setVisibility(View.GONE); UI.imgv_user_rank.setVisibility(View.VISIBLE); if (item.getRank() != null || !TextUtils.isEmpty(item.getRank())) { //Picasso.with(G.context).load(item.getRank()).into(UI.imgv_user_rank); } UI.tv_person_nickname_mobile_number.setText(item.getNick_name()); //UI.ll_root.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.selector_button_actions)); if (item.getContact_image() == null || TextUtils.isEmpty(item.getContact_image())) { Bitmap bitmap = UC.getContactPhoto(item.getMobile_number(), G.context.getContentResolver()); if (bitmap != null) { UI.img_contact_image.setImageBitmap(bitmap); } else { UI.img_contact_image.setImageDrawable(G.context.getResources().getDrawable(R.drawable.no_avatar)); } } else { // show user avatar from web //Picasso.with(G.context).load(item.getContact_image()).into(UI.img_contact_image); UI.img_contact_image.setImageBitmap(BitmapFactory.decodeFile(G.dir_image + "/" + item.getContact_image())); } } else { // UI.ll_root.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.selector_invite_actions)); UI.btn_invite_message.setVisibility(View.VISIBLE); UI.imgv_user_rank.setVisibility(View.GONE); UI.btn_invite_message.setText(UC.getString(R.string.invite_person)); UI.btn_invite_message.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.shape_invite_button_default)); UI.tv_person_nickname_mobile_number.setText(item.getMobile_number()); Bitmap bitmap = UC.getContactPhoto(item.getMobile_number(), G.context.getContentResolver()); if (bitmap != null) { UI.img_contact_image.setImageBitmap(bitmap); } else { UI.img_contact_image.setImageDrawable(G.context.getResources().getDrawable(R.drawable.no_avatar)); } } } } @Override public Filter getFilter() { if (filter == null) { filter = new NameFilter(); } return filter; } public class NameFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); String searchText = constraint.toString().toLowerCase(); ArrayList<ContactLists> newList = filterListBasedOnSearchText(searchText); results.values = newList; results.count = newList.size(); return results; } private ArrayList<ContactLists> filterListBasedOnSearchText(String constraint) { ArrayList<ContactLists> newList = new ArrayList<ContactLists>(); int l = originalList.size(); for (int i = 0; i < l; i++) { ContactLists nameList = originalList.get(i); if (nameList.getContact_name().toString().contains(constraint)) { newList.add(nameList); } } return newList; } @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { categoryArrayList = (ArrayList<ContactLists>) results.values; notifyDataSetChanged(); } } } 的清单中的软键盘状态状态。这个类有视图分页器,有三个片段:

ActivityMain

其他方式在没有适配器功能的情况下进行片段过滤

    <activity android:name=".Activities.ActivityBootstrap" android:windowSoftInputMode="adjustPan"  android:screenOrientation="portrait"/>

当我关闭或隐藏用nw项刷新的软键时,ListView成功过滤。

4 个答案:

答案 0 :(得分:2)

您从我发布的代码中看到,您没有使用适配器过滤器。我将在这里发布一个示例过滤器以及如何调用它(保留所有变量名称以使其更容易)。

适配器类中的'NameFilter'类:

public class NameFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();

        String searchText = constraint.toString().toLowerCase();
        ArrayList<ContactLists> newList = filterListBasedOnSearchText(searchText);
        results.values = newList;
        results.count = newList.size();

        return results;
    }

    private ArrayList<ContactLists> filterListBasedOnSearchText(String constraint) {
        ArrayList<ContactLists> newList = new ArrayList<ContactLists>();

        int l = originalList.size();
        for (int i = 0; i < l; i++) {
            ContactLists nameList = originalList.get(i);

            if (nameList.getContact_name().toString().contains(constraint)) {
                newList.add(nameList);
            }
        }

        return newList;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        categoryArrayList = (ArrayList<ContactLists>) results.values;
        notifyDataSetChanged();
    }
}

列表片段中的'TextWatcher'接口方法实现:

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

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

public void afterTextChanged(Editable s) {
    String text = searchView.getText().toString();
    NameFilter itemFilter = (NameFilter) adapter.getFilter();
    itemFilter.filter(text);
}

另外,一些观察结果:

  • 如果'ContactLists'是联系人,请将其命名为'联系',以避免混淆
  • 我会使用'SearchView',而不是'EditText'
  • 我不知道您是如何获得联系人列表的,但是有关于它的指南here(如果您还没有查看)

答案 1 :(得分:0)

实施起来非常简单。请按照以下步骤操作:

第1步: 初始化Edittext:

$scope.data_new = [];
for(i in $scope.data){
  $scope.data_new.push(
   { id: $scope.data[i].id, name: $scope.data[i].name }
  )
}

第2步: 在此edittext上添加TextChangedListener。像:

     editTextVideoFolderSearch = (EditText) rootView.findViewById(R.id.editTextVideoFolderSearch);

步骤3:您必须使用不同的bean来填充列表视图中的适配器:

editTextVideoFolderSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence t, int start, int before,
                int count) {
            videoDetailBeansSearch.clear();
                for (int i = 0; i < videoDetailBeans_total.size(); i++) {
                    if (videoDetailBeans_total.get(i).getAlbum().toLowerCase()
                            .contains(t)) {
                        videoDetailBeansSearch.add(videoDetailBeans_total.get(i));
                    }
                }
                if (videoDetailBeansSearch.size() > 0) {
                    video_folder_gridView.setAdapter(new AlbumGridAdapter(getActivity(), videoDetailBeansSearch));
                } else {
                    video_folder_gridView.setAdapter(new AlbumGridAdapter(getActivity(), videoDetailBeansSearch));
                }
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

步骤4:在加载活动或片段时列出列表:

    private ArrayList<MediaDetailBean> videoDetailBeansSearch = new ArrayList<MediaDetailBean>();
    private ArrayList<MediaDetailBean> videoDetailBeans_total = new ArrayList<MediaDetailBean>();
    private ArrayList<MediaDetailBean> videoDetailBeans_delete = new ArrayList<MediaDetailBean>();

步骤5:将listview适配器设置为:

public class GetVideoAsynctask extends AsyncTask<String, String,Object[]> {

        ProgressDialog progressDialog ;
        String response = "";
        int offset, limit;

        public GetVideoAsynctask(int offset1, int limit1) {
          offset = offset1;
          limit = limit1;
        }

        @Override
        public void onPreExecute() {
            progressDialog = ProgressDialog.show(getActivity(),"Loading...", "Please Wait");
        }

        @Override
        public Object[] doInBackground(String... params) {

            return AppParsing.getMediaDetails(getActivity(), AppPreferenceUtils.getChildUploadDeviceId(getActivity()),
                                AppPreferenceUtils.getChildRaId(getActivity()),"2", offset, limit,"3"); 
        //// type=1 for image,2 for video,3 for audio
        }  

        @SuppressWarnings("unchecked")
        @Override
        public void onPostExecute(Object[] result) {
            progressDialog.cancel();
            try
            {
                boolean status = (Boolean) result[0];
                response = (String) result[1];

                if(status)
                {
                    videoDetailBeans = (ArrayList<MediaDetailBean>) result[2] ; 

                    for (int i = 0; i < videoDetailBeans.size(); i++) {
                        videoDetailBeans_total.add(videoDetailBeans.get(i));
                    }

                    isCheck_video = new boolean[videoDetailBeans_total.size()];

                    for(int i=0;i<videoDetailBeans_total.size();i++)
                    {
                        if(!GRID_DATA.contains(videoDetailBeans_total.get(i).getAlbum()))
                        {
                            GRID_DATA.add(videoDetailBeans_total.get(i).getAlbum());
                        }
                    }

                    video_folder_gridView.setAdapter(new AlbumGridAdapter(getActivity(), videoDetailBeans_total));

                    //Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show();
                }
                else
                {
                    response = (String) result[1];
                    AppUtils.showDialog(getActivity(), response);
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

第6步:最重要的细化是我在这里加载更多列表视图:

new GetVideoAsynctask(offset1, limit1).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

第7步:在我的xml中我声明如下:

com.costum.android.widget.LoadMoreListView) video_folder_gridView = (com.costum.android.widget.LoadMoreListView)     rootView.findViewById(R.id.video_folder_gridView);

如果您对此概念有任何疑问,请随意。

答案 2 :(得分:0)

这是我的Filter实现,它在您输入单个字母时进行过滤。这是Activity实现...

 public class MessagesActivity extends Activity{

       private ListView msgListView;
       private EditText mSearchContent;
       private MessagesAdapter msgAdapter;
       public static ArrayList<MessagesBean> allMsgsList = new ArrayList<MessagesBean>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_messages);
            init();
            msgAdapter = new MessagesAdapter(getApplicationContext(), allMsgsList, MessagesActivity.this);
            msgListView.setAdapter(msgAdapter);
    }
    private void init() {
            // TODO Auto-generated method stub
            msgListView = (ListView) findViewById(R.id.messages_list);
            msgListView.setOnScrollListener(this);


            mSearchContent.addTextChangedListener(new TextWatcher() {
                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                }

                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                }

                public void afterTextChanged(Editable s) {

                    msgAdapter.getFilter().filter(s.toString());
                }
            });


}

这是适配器实现

public class MessagesAdapter extends BaseAdapter implements Filterable {

    private Context mcontext;
    private ArrayList<MessagesBean> all_details, dup_all_details;
    private LayoutInflater inflater;
    private DisplayImageOptions options;
    private CustomClickLisntener clickLisntener;

    private MessagesBean mMessagesBean;
    private ViewHolder selectedHolder;
    private ListViewFilter listviewFilter;

    // int pos = 0;
    public MessagesAdapter(Context context, ArrayList<MessagesBean> all_list, CustomClickLisntener lisntener) {
        mcontext = context;
        all_details = all_list;

        clickLisntener = lisntener;
        inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build();
        dup_all_details = all_list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return all_details.size();
    }

    @Override
    public MessagesBean getItem(int position) {
        // TODO Auto-generated method stub
        return all_details.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    static class ViewHolder {
        TextView user_name, msg_sub, msg_content, msg_time;
        ImageView user_image;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewholder = null;
        if (convertView == null) {
            viewholder = new ViewHolder();
            convertView = inflater.inflate(R.layout.row_item_inbox_msg, null);

            viewholder.user_name = (TextView) convertView.findViewById(R.id.user_name);
            viewholder.msg_time = (TextView) convertView.findViewById(R.id.msg_time);
            viewholder.msg_sub = (TextView) convertView.findViewById(R.id.msg_subject);
            viewholder.user_image = (ImageView) convertView.findViewById(R.id.user_image);
            viewholder.msg_content = (TextView) convertView.findViewById(R.id.msg_content);

            // viewholder.down_arrow.setTag(position);
            convertView.setTag(viewholder);

        } else {
            viewholder = (ViewHolder) convertView.getTag();
        }

        mMessagesBean = all_details.get(position);
        viewholder.user_name.setText(mMessagesBean.getFirstname().trim() + " " + mMessagesBean.getLastname());
        viewholder.msg_time.setText(DateDifferent.getDateDifferance(mMessagesBean.getSentDate()));
        viewholder.msg_sub.setText(mMessagesBean.getSubject());
        viewholder.msg_content.setText(mMessagesBean.getMessage());

        ImageLoader.getInstance().displayImage(mMessagesBean.getUserimage(), viewholder.user_image, options);

        return convertView;
    }

    @Override
    public Filter getFilter() {
        // TODO Auto-generated method stub
        if (listviewFilter == null) {
            listviewFilter = new ListViewFilter();

        }
        return listviewFilter;
    }

    public class ListViewFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            // TODO Auto-generated method stub
            constraint = constraint.toString();

            ArrayList<MessagesBean> allitems = new ArrayList<MessagesBean>();

            FilterResults filterresults = new FilterResults();

            if (constraint != null && constraint.toString().length() > 0) {

                for (int i = 0; i < dup_all_details.size(); i++) {

                    if (dup_all_details.get(i).getFirstname().contains(constraint)
                            || dup_all_details.get(i).getLastname().contains(constraint)) {
                        allitems.add(dup_all_details.get(i));

                    }

                }

                filterresults.count = allitems.size();
                filterresults.values = allitems;

            } else {

                System.out.println(" iam here..." + all_details.size());

                synchronized (this) {
                    filterresults.count = allitems.size();
                    filterresults.values = allitems;
                }

            }

            return filterresults;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            // TODO Auto-generated method stub

            all_details = (ArrayList<MessagesBean>) results.values;
            MessagesActivity.allMsgsList = all_details;
            MessagesAdapter.this.notifyDataSetChanged();

        }

    }

}

答案 3 :(得分:0)

你可以这样做:

public void filter(String charText) {
    drinks.clear();
    if (charText.length() == 0) {
        drinks.addAll(contact_list);
    } else {
        for (ContactLists wp : contact_list) {
            if (wp.getContact_name().contains(charText)) {
                drinks.add(wp);
            }
        }
    }
    contact_list.clear();
    contact_list.addAll(drinks);
    adapter.notifyDataSetChanged(contact_list);
}

并在您的适配器中:

public void notifyDataSetChanged(List<ContactLists> items){
   this.categoryArrayList = items;
   super.notifyDataSetChanged();
}