Question

In my application, I have to show a ListView, each row of the listview should hold one GridLayout. And the items for the gridlayout are dynamically added through the adapter class.

@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;
    }

But i couldn't see any grid items. Please provide me the correct way to implement

Was it helpful?

Solution

In getChildView() i write this code. Now its display the grid. But I dont want to use hardcoded value.

child.setLayoutParams(new GridLayout.LayoutParams(new LayoutParams(250,300)));

Any efficient answers are appreciated

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top