我的应用程序要求我显示一个动态嵌套列表视图。我的外部列表视图是一系列工作,每个工作都包含一个列表视图,其中包含它所需的行项目。我正在使用自定义适配器来实现动态嵌套列表视图。如下所示:
LineItemCustomListViewAdaptor - 加载单个作业的数据项
JobCustomListViewAdaptor - 通过在每个作业上调用 LineItemCustomListViewAdaptor 来加载各个作业。
我的 JobCustomListViewAdaptor 的 getView 函数是这样的-
public View getView(int position, View convertView, ViewGroup parent ) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_job, parent, false);
holder = new ViewHolder();
holder.jobnum = (TextView)convertView.findViewById(R.id.JobNum); // jobID
holder.lineitems = (ListView)convertView.findViewById(R.id.list); // lineitems
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.jobnum.setText(job.getJobID());
listviewadapter = new LineItemCustomListViewAdapter(context,R.layout.list_row, job.getLineItem());
holder.lineitems.setAdapter(listviewadapter);
return convertView;
}
这在显示外部作业列表和内部列表之前都可以正常工作。但是内部的每个作业只显示第一个产品项目,即外部列表的每一行只显示内部列表的第一行。
谁能指出我是否遗漏了什么或哪里出错了。任何帮助将不胜感激!
编辑:根据 Barend 的评论,使用 ExpandableListAdapter 使其工作。
由于 ExpandableListView 根据需要将每个元素添加为父/子组中的新行,因此列表的动态性质并不重要。我将父对象及其关联的子对象数据存储在扩展适配器类中处理的 ArrayList 对象中。
public class ExpandableAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<String> parent;
private ArrayList<ArrayList<LineItem>> children;
public ExpandableAdapter(Context context, ArrayList<String> parent,
ArrayList<ArrayList<LineItem>> children) {
this.context = context;
this.parent = parent;
this.children = children;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final LineItem lineitem = (LineItem)getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_row, null);
}
TextView item; // item
TextView desc; // desc
item = (TextView)convertView.findViewById(R.id.Item); // item
desc = (TextView)convertView.findViewById(R.id.Desc); // desc
item.setText(lineitem.getItem());
desc.setText(lineitem.getDesc());
return convertView;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Integer parentnum = Integer.valueOf(groupPosition);
String parentcond = (String) getGroup(groupPosition);
TextView jobnum; // jobID
TextView cond;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_job,
null);
}
jobnum = (TextView)convertView.findViewById(R.id.JobNum); // jobID
cond = (TextView)convertView.findViewById(R.id.Condition); // cond
jobnum.setText(parentnum.toString())
cond.setText(parentcond);
return convertView;
}
/// other methods
}