使用适配器

时间:2015-08-27 18:28:03

标签: android listview android-fragments android-listview android-adapter

我有一个片段。这个片段在他的onCreateView方法上加载的视图有一个ListView(A)(填充在Adapter(A)中)。但是,这个ListView(A)里面有另一个ListView(B)。所以现在,我必须调用适配器(B)来填充此列表视图(B)。如果我从片段中调用它,我会得到一个空指针,如果我从适配器(A)调用它,它不会崩溃,但它不起作用。

如何在另一个内部调用适配器。

这是Fragment的代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mContentView = inflater.inflate(R.layout.fragment_kidscontacts, container, false);
    mAdapter = new KidsContactsAdapter(getActivity());
    final ListView list = (ListView) mContentView.findViewById(R.id.listViewKidsContacts);
    list.setAdapter(mAdapter); 
    return mContentView;
}

其中mAdapter是对我制作的适配器(A)的调用。而mContentView是一个简单的视图。 现在,适配器(A)的代码,我有一个名为holder的Holder对象(它保存了xml中的元素)。

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder; //this is the holder object I created

    if (convertView == null) { //View received from parameter.
        convertView = LayoutInflater.from(mContext).inflate(R.layout.kids_contacts_list_item, parent, false);
        holder = new ViewHolder(); 
        holder.listViewKidsContacts = (ListView) convertView.findViewById(R.id.insidelistViewKidsContacts); //ListView(B)
        holder.kidImage = (ImageView) convertView.findViewById(R.id.kid_image);
        holder.statusImageView = (ImageView) convertView.findViewById(R.id.status_image_view);
        holder.nameTxtView = (TextView) convertView.findViewById(R.id.kid_friend_txtview);
        holder.statusTextView = (TextView) convertView.findViewById(R.id.kid_status_txtview);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    //Some work I do with the holder items assigning them 

    //Trying to fill the ListView(B)
        KidsInsideContactsAdapter mAdapter = new KidsInsideContactsAdapter(((Activity)mContext)); //This is the adapter(B)
        holder.listViewKidsContacts.setAdapter(mAdapter); //Assign the adapter(B) to the ListView(B)

    return convertView;
}

我在适配器(B)中有一些日志,但它们没有显示在LogCat中。列表视图也没有填写。它显示就好像没有列表视图。但是在xml中就是这样。

2 个答案:

答案 0 :(得分:5)

可能发生的一件事是第二个列表视图的高度未正确设置,因为它位于另一个列表视图中。我可能不得不处理一次。

尝试在第一个适配器中使用此方法设置列表视图的高度 - (在您设置适配器之后) 像setListViewHeightBasedOnChildren(holder.listViewKidsContacts);

//动态设置ListView高度的方法

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

如果这不起作用,我会尝试在行上划线:  KidsInsideContactsAdapter mAdapter = new KidsInsideContactsAdapter(((Activity)mContext)); //这是适配器(B)         holder.listViewKidsContacts.setAdapter(mAdapter); //将适配器(B)分配给ListView(B)

调试时检查mAdapter是否为null。还在KidsInsideContactsAdapter的构造函数中放置断点,以查看是否一切正常。

还要检查以确保holder.listViewKidsContacts是您在调试时要查找的列表视图。

答案 1 :(得分:1)

你是对的。要填充第二个列表视图,请在

中的第一个列表视图的适配器中填充

@覆盖     public View getView(final int position,View convertView,ViewGroup parent){

方法(在第一个列表视图中获取每个项目),您必须从视图中获取(第二个)列表视图(convertView或为第一个列表视图的项目膨胀布局)。一旦你抓住你想要填充的(第二个)列表视图,在这个方法中,你可以设置适配器,例如: ListView listView =(ListView)view.findViewById(R.id .___); //获取第一个列表视图中的第二个列表视图 listView.setAdapter(您的第二个列表视图的适配器)

这应该有效。把你的代码放在这里,我可以看看它,并告诉你为什么它可能无法正常工作。

另请查看以下内容:(如果您尝试做类似的事情)

https://github.com/emilsjolander/StickyListHeaders

或者 How to make sticky section headers (like iOS) in Android?

相关问题