Lok*_*kio 6 android android-arrayadapter android-layout android-listview
如何填充已经位于自定义适配器行内的列表视图。这是正确的方法吗?
我有一个组织对象。而且这个组织可以有更多的联系。
例如,我想创建一个布局,顶部是组织名称,然后是其下的联系人名称。
获取组织对象并将其放入我的自定义适配器并显示名称没有问题。但是如何在该行布局中填充列表视图?我并没有真正获得自定义适配器内的新 ArrayAdapter 构造函数的上下文参数。它需要什么?
public class ContactAdapter extends ArrayAdapter<Organization> {
Context context;
int layoutResourceId;
ArrayList<Organization> data = null;
public ContactAdapter(Context context, int layoutResourceId, ArrayList<Organization> data) {
super(context, layoutResourceId, data);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
OrganizationHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent,false);
holder = new OrganizationHolder();
holder.tvOrgName = (TextView)row.findViewById(R.id.tvOrgName);
holder.LvContacts = (ListView)row.findViewById(R.id.LvContacts);
row.setTag(holder);
} else {
holder = (OrganizationHolder) row.getTag();
}
Organization org = data.get(position);
ArrayList<String> contacts = new ArrayList<String>();
for (Contact c : org.getContacts()) {
contacts.add(c.getName());
}
Log.i("List",""+contacts);
ArrayAdapter<String> contactsadapter = new ArrayAdapter<String>(??,android.R.layout.simple_list_item_1,contacts);
holder.LvContacts.setAdapter(contactsadapter);
holder.tvOrgName.setText(org.getName());
return row;
}
static class OrganizationHolder {
TextView tvOrgName;
ListView LvContacts;
Button btnDetails;
}
Run Code Online (Sandbox Code Playgroud)
您不应该将 ListView 放在另一个 ListView 的行中。
您可能会考虑使用ExpandableListViewand subclassBaseExpandableListAdapter而不是您现在使用的类。使用组织作为“组”视图,使用联系人作为“子”视图。
如果您不需要可扩展的列表并且绝对需要一个适配器中的所有信息,则可以形成一个数据结构,该数据结构是组织的地图(或列表),每个组织都有一个联系人列表。然后您将编写一个自定义适配器实现来使用它。
public class MyAdapter extends BaseAdapter {
private List<Organization> mList;
private LayoutInflater mInflater;
private int mCount;
public MyAdapter(Context context, List<Organization> list) {
mInflater = LayoutInflater.from(context);
mLIst = list;
mCount = countItems();
}
private int countItems() {
int count = 0;
if (list != null) {
for (Organization org : organizations) {
count++; // org
List<Contact> contacts = org.getContacts();
if (contacts != null) count += contacts.size()); // contacts
}
}
return count;
}
public int getCount() {
return mCount;
}
public Object getItem(int position) {
for (Organization org : orgnizations) {
if (position == 0) {
return org;
}
position--;
List<Contact> contacts = org.getContacts();
if (contacts != null) {
int size = contacts.size();
if (position >= size) {
position -= size;
} else {
return contacts.get(position);
}
}
}
// not found
throw new IndexOutOfBoundsException(); // or some other exception
}
// other methods omitted for brevity
}
Run Code Online (Sandbox Code Playgroud)
我留下了我们的getView()方法,但希望你能自己解决这个问题。如果您的行需要根据显示的数据(组织或联系人)而看起来不同,您还应该实现getItemViewType()和getItemViewTypeCount()。