【发布时间】:2015-11-22 06:43:33
【问题描述】:
我有一个片段。这个片段在他的 onCreateView 方法上加载的视图有一个 ListView(A)(在 Adapter(A) 中填充)。但是,这个 ListView(A) 里面还有另一个 ListView(B)。所以现在,我必须调用一个适配器(B)来填充这个列表视图(B)。如果我从片段中调用它,我会得到一个空指针,如果我从适配器(A)中调用它,它不会崩溃,但它不起作用。
如何在另一个适配器中调用适配器。
这是片段的代码:
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 是对我所做的 Adapter(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中有。
【问题讨论】:
标签: android listview android-fragments android-listview android-adapter