过滤器自定义列表视图使用BaseAdapter [英] Filter Custom Listview Using BaseAdapter

查看:108
本文介绍了过滤器自定义列表视图使用BaseAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想过滤使用BaseAdapter和非常失败至今的自定义列表视图。我想我需要一个新的一双眼睛。我通过各种问题转眼就SO搜查了很多谷歌了。而且,我已经试过到目前为止一切都失败了。

I am trying to filter a custom Listview that uses BaseAdapter and have failed miserably so far. I think I need a fresh pair of eyes. I have gone through various questions on SO and searched a lot on Google too. And everything that I have tried so far has failed.

我获取来自Facebook用户的好友列表,铸造成果转化为一个ArrayList和捆绑成的String []的适配器。

I am fetching the users Friends List from Facebook, casting the results into an Arraylist and bundling them into String[]'s for the Adapter.

活动code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friends_list);

        arrayUID = new ArrayList<String>();
        arrayName = new ArrayList<String>();
        arrayPicture = new ArrayList<String>();
        arrayInfo = new ArrayList<String>();

        Bundle extras = getIntent().getExtras();
        apiResponse = extras.getString("API_RESPONSE");
        graph_or_fql = extras.getString("METHOD");

        try {
            JAFriends = new JSONArray(apiResponse);

            for (int i = 0; i < JAFriends.length(); i++) {
                json_data = JAFriends.getJSONObject(i);

//              mainAlbumID = json_data.getString("id");

                if (json_data.has("uid"))   {
                    String getFriendID = json_data.getString("uid");
                    arrayUID.add(getFriendID);
                } else {
                    String getFriendID = null;
                    arrayUID.add(getFriendID);
                }


                if (json_data.has("name"))  {
                    String getFriendName = json_data.getString("name");
                    arrayName.add(getFriendName);
                } else {
                    String getFriendName = null;
                    arrayName.add(getFriendName);
                }

                if (json_data.has("current_location"))  {
                    try {
                        JSONObject location = json_data.getJSONObject("current_location");
                        String friendLocationDetails = location.getString("city") + ", " + location.getString("state");
                        arrayInfo.add(friendLocationDetails);
                    } catch (JSONException e)   {
                        arrayInfo.add("");
                    }
                } else {
                    arrayInfo.add("");
                }


                if (json_data.has("pic_square"))    {
                    String getFriendPhoto = json_data.getString("pic_square");
                    arrayPicture.add(getFriendPhoto);
                } else {
                    String getFriendPhoto = null;
                    arrayPicture.add(getFriendPhoto);
                }
            }
        } catch (JSONException e) {
            return;
        }

        stringUID = new String[arrayUID.size()];
        stringUID = arrayUID.toArray(stringUID);

        stringName = new String[arrayName.size()];
        stringName = arrayName.toArray(stringName);

        stringPicture = new String[arrayPicture.size()];
        stringPicture = arrayPicture.toArray(stringPicture);

        stringInfo = new String[arrayInfo.size()];
        stringInfo = arrayInfo.toArray(stringInfo);

        listofFriends = (ListView)findViewById(R.id.list);
        adapter = new FriendsAdapter(this, stringUID, stringName, stringPicture, stringInfo, arrayName);

        listofFriends.setTextFilterEnabled(true);
        listofFriends.setAdapter(adapter);

        filterText = (EditText) findViewById(R.id.editFilterList);
        filterText.addTextChangedListener(filterTextWatcher);
    }

    private TextWatcher filterTextWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            adapter.getFilter().filter(s.toString());
//          adapter.notifyDataSetChanged();
//          listofFriends.setAdapter(adapter);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    };

我的适配器类:

My adapter class:

public class FriendsAdapter extends BaseAdapter implements Filterable {

    Activity activity;
    String[] fetFriendID;
    String[] fetFriendName;
    String[] fetFriendPicture;
    String[] fetFriendInfo;

    List<String> arrayList;
    List<String> mOriginalValues;

    LayoutInflater inflater = null;
    ProfilePictureLoader profileLoader;

    FriendsAdapter(Activity a, String[] stringUID, String[] stringName, 
            String[] stringPicture, String[] stringInfo, ArrayList<String> arrayName) {

        activity = a;
        fetFriendID = stringUID;
        fetFriendName = stringName;
        fetFriendPicture = stringPicture;
        fetFriendInfo = stringInfo;

        arrayList = new ArrayList<String>();
        mOriginalValues = arrayName;

        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        profileLoader = new ProfilePictureLoader(activity.getApplicationContext());
    }


    public int getCount() {
        return mOriginalValues.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }


    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if(convertView == null)
            vi = inflater.inflate(R.layout.friends_item, null);

        ImageView imgProfilePicture = (ImageView)vi.findViewById(R.id.profile_pic);
        TextView txtFriendsName = (TextView)vi.findViewById(R.id.name);
        TextView txtFriendsInfo = (TextView)vi.findViewById(R.id.info);

        // SET THE CUSTOM FONTS
        Typeface nameHeader             = Typeface.createFromAsset(activity.getAssets(), "fonts/museo_slab_700_headers.otf");
        Typeface tfContent              = Typeface.createFromAsset(activity.getAssets(), "fonts/Cabin-Medium-TTF.ttf");
        txtFriendsName.setTypeface(nameHeader);
        txtFriendsInfo.setTypeface(tfContent);

        txtFriendsName.setText(fetFriendName[position]);

        txtFriendsInfo.setText(fetFriendInfo[position]);

        if (fetFriendPicture[position] != null){
            profileLoader.DisplayImage(fetFriendPicture[position], imgProfilePicture);
        }
        else if (fetFriendPicture[position] == null) {
            imgProfilePicture.setVisibility(View.GONE);
        }

        return vi;
    }

    @Override
    public Filter getFilter() {

        Filter filter = new Filter() {

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

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                FilterResults results = new FilterResults();
                List<String> FilteredArrList = new ArrayList<String>();

                if (constraint == null || constraint.length() == 0) {
                    results.count = mOriginalValues.size();
                    results.values = mOriginalValues;
                } else {
                    constraint = constraint.toString();

                    for (int i = 0; i < mOriginalValues.size(); i++) {
                        String data = mOriginalValues.get(i);
                        if (data.toLowerCase().startsWith(constraint.toString()))   {
                            FilteredArrList.add(data);
                        }
                    }

                    results.count = FilteredArrList.size();
                    results.values = FilteredArrList;
                }

                return results;
            }
        };

        return filter;
    }
}

我知道这是在它吨code一个浩浩荡荡的问题。如果你花时间去通读整个帖子的时间,所以,谢谢你。

I know it's a mighty long question with tons of code in it. So if you take the time to read through the entire post, thank you.

我真的希望有人在这里SO可以帮助/指导我这个问题。

I really hope someone here on SO can assist / guide me on this problem.

推荐答案

只是通过取值过滤()。

adapter.getFilter().filter(s);

另外解决您的的getItem 方法。

public Object getItem(int position) {
    return mOriginalValues.get(position);
}

这篇关于过滤器自定义列表视图使用BaseAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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