ListView没有显示任何内容,但在单击搜索视图时显示

时间:2016-04-20 10:39:26

标签: android android-viewpager baseadapter searchview

我有一个名为FriendListActivity的活动,它包含3个选项卡,即CHAT,GROUP和CONTACTS.FriendListActivity包含一个viewpager,它使用FragmentStatePagerAdapter通过Fragments填充。现在每个片段都包含一个使用BaseAdapter填充的列表视图。

以下是FriendListActivity中的SearchView代码:

  SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        public boolean onQueryTextChange(String query) {
            // this is your adapter that will be filtered
            Log.e("Text", query);
            PagerAdapter pagerAdapter = (PagerAdapter) viewPager.getAdapter();
            for (int i = 0; i < pagerAdapter.getCount(); i++) {

                Fragment viewPagerFragment = (Fragment) viewPager.getAdapter().instantiateItem(viewPager, i);
                if (viewPagerFragment != null && viewPagerFragment.isAdded()) {

                    if (viewPagerFragment instanceof ChatFragment) {
                        ChatFragment chatFragment = (ChatFragment) viewPagerFragment;
                        if (chatFragment != null) {
                            chatFragment.beginSearch(query); // Calling the method beginSearch of ChatFragment
                        }
                    } else if (viewPagerFragment instanceof GroupsFragment) {
                        GroupsFragment groupsFragment = (GroupsFragment) viewPagerFragment;
                        if (groupsFragment != null) {
                            groupsFragment.beginSearch(query); // Calling the method beginSearch of GroupsFragment
                        }
                    } else if (viewPagerFragment instanceof ContactsFragment) {
                        ContactsFragment contactsFragment = (ContactsFragment) viewPagerFragment;
                        if (contactsFragment != null) {
                            contactsFragment.beginSearch(query); // Calling the method beginSearch of ContactsFragment
                        }
                    }
                }
            }
            return false;
        }

        public boolean onQueryTextSubmit(String query) {
            //Here u can get the value "query" which is entered in the search box.
            return false;
        }
    };

AdapterFriendList.java

以下代码填充了ChatFragment的列表视图:

public class Adapter_FriendsList extends BaseAdapter implements Filterable {
private Context context;
private List<Bean_Friends> listBeanFriends;
private LayoutInflater inflater;
private ApiConfiguration apiConfiguration;
List<Bean_Friends> mStringFilterList;
ValueFilter valueFilter;

public Adapter_FriendsList(Context context, List<Bean_Friends> listBeanFriends) {
    this.context = context;
    this.listBeanFriends = listBeanFriends;
    mStringFilterList = listBeanFriends;
}

@Override
public int getCount() {
    return listBeanFriends.size();
}

@Override
public Object getItem(int position) {
    return listBeanFriends.get(position);
}

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

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    Log.e("GetView", "Called");
    if (inflater == null)
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (view == null)
        view = inflater.inflate(R.layout.feed_item_chat_list, null);

    //Getting Views
    ImageView img_friend = (ImageView) view.findViewById(R.id.imgFriend);
    TextView txtNameFriend = (TextView) view.findViewById(R.id.txtNameFriend);
    // ImageView imgMoreOption = (ImageView) view.findViewById(R.id.imgMoreoption);
    TextView txtFriendsID = (TextView) view.findViewById(R.id.txtFriendsID);

  /*  imgMoreOption.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Dialog dialog = new Dialog(context);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // hide the title bar
            dialog.setContentView(R.layout.more_option);
            dialog.show();
        }
    });*/

    //Setting values in the views
    //Data source of Adpater is List.At a specific position in the list ,we get Bean_Friends object
    Bean_Friends bean_friends = listBeanFriends.get(position);
    //Getting values from Bean_Friends
    String name = bean_friends.getName();
    Log.e("FriendNAmeAD", name);
    String url = bean_friends.getUrl();
    Log.e("urlAD", url);
    String extension = bean_friends.getExtension();
    String friendID = bean_friends.getFriendsID();

    //Initializing ApiConfiguration
    apiConfiguration = new ApiConfiguration();
    String api = apiConfiguration.getApi();
    String absoluteURL = api + "/" + url + "." + extension;
    Log.e("AbsoluteURLAD", absoluteURL);

    Picasso.with(context).load(absoluteURL).error(R.drawable.default_avatar).into(img_friend); //Loading image into the circular Image view using Picasso
    txtNameFriend.setText(name);//Setting name
    txtFriendsID.setText(friendID);
    return view;
}

public static boolean exists(String URLName) {
    try {
        HttpURLConnection.setFollowRedirects(false);
        // note : you may also need
        //        HttpURLConnection.setInstanceFollowRedirects(false)
        HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
        con.setRequestMethod("HEAD");
        return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

@Override
public Filter getFilter() {
    if (valueFilter == null) {
        valueFilter = new ValueFilter();
    }
    return valueFilter;
}


private class ValueFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String str = constraint.toString().toUpperCase();
        Log.e("constraint", str);
        FilterResults results = new FilterResults();

        if (constraint != null && constraint.length() > 0) {
            ArrayList<Bean_Friends> filterList = new ArrayList<Bean_Friends>();
            for (int i = 0; i < mStringFilterList.size(); i++) {
                if ((mStringFilterList.get(i).getName().toUpperCase())
                        .contains(constraint.toString().toUpperCase())) {
                    Bean_Friends bean_friends = mStringFilterList.get(i);
                    filterList.add(bean_friends);
                }
            }
            results.count = filterList.size();
            results.values = filterList;
        } else {
            results.count = mStringFilterList.size();
            results.values = mStringFilterList;
        }
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        listBeanFriends = (ArrayList<Bean_Friends>) results.values;
        notifyDataSetChanged();
    }
}

}

我的问题是当我点击CHAT选项卡时,没有显示任何内容,但是当我点击包含聊天选项卡的活动上显示的SearchView时,会显示列表视图的内容。我希望显示CHAT选项卡的内容当用户按下滑动屏幕时。请帮我解决问题。

0 个答案:

没有答案