0
在我的應用程序中,我必須顯示一個ListView,listview的每一行應該保存一個GridLayout。而網格佈局的項目則通過適配器類動態添加。Android:將自定義視圖添加到空GridLayout
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
// if it's not create convertView yet create new one and consume it
if (convertView == null) {
// instantiate convertView using our list_item
convertView = inflater.inflate(R.layout.order_grid_list_view, null);
// get new ViewHolder
holder = new ViewHolder();
holder.order_row = (GridLayout) convertView
.findViewById(R.id.order_row);
holder.order_no = (TextView) convertView
.findViewById(R.id.order_no);
holder.order_date = (TextView) convertView
.findViewById(R.id.order_date);
holder.complete = (Button) convertView
.findViewById(R.id.order_btn);
}
// if it's exist convertView then consume it
else {
Log.d("ListAdaptor", "convertView is not null");
holder = (ViewHolder) convertView.getTag();
}
orderno_ = data.get(position).getNo() + "";
// set data to holder
holder.order_no.setText(data.get(position).getNo() + "");
holder.order_date.setText(data.get(position).getDateTime());
if (mode.equalsIgnoreCase("Y")) {
holder.complete.setText("InComplete");
} else {
holder.complete.setText("Complete");
holder.complete.setBackgroundDrawable(context.getResources()
.getDrawable(R.drawable.green_button_bg));
}
for (int i = 0; i < data.get(position).getItems().size(); i++) {
holder.order_row.addView(getChildView(position, i));
}
holder.complete.setOnClickListener(new ButtonOnClickListener(
clickListener, orderno_));
return convertView;
}
private View getChildView(int pos, int i) {
View child = inflater.inflate(R.layout.order_item, null);
InnerViewHolder inholder = new InnerViewHolder();
inholder.name = (TextView) child.findViewById(R.id.order_name);
inholder.items = (ListView) child.findViewById(R.id.order_list);
inholder.name.setText(data.get(pos).getItems().get(i).getItemName());
inholder.items.setAdapter(new OrderItemOptionAdapter(context, data
.get(pos).getItems().get(i).getOptions()));
return child;
}
static class ViewHolder {
GridLayout order_row;
TextView order_no, order_date;
Button complete;
}
static class InnerViewHolder {
TextView name;
ListView items;
}
但我看不到任何網格物品。請給我提供正確的實現方法
我已在XML一些變化,現在它的表現 – Sridhar