Linearlayout into cell of gridview

83 views Asked by At

I have got a problem with programmatically created LinearLayout contains into design time created GridView.

public class page2 extends Fragment
{
    public View rootView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.page2, container, false);

        String [] rings=
            {
                    "1 пара", "8.30 - 10.05", "8.30 - 9.15 \n 9.20 - 10.05",
                    "2 пара", "10.15 — 11.50", "10.15 - 11.00 \n 11.05 - 11.50",
                    "3 пара", "12.30 — 14.05", "12.30 - 13.15 \n 13.20 - 14.05",
                    "4 пара", "14.15 — 15.50", "14.15 - 15.00 \n 15.05 - 15.50",
                    "5 пара", "16.00 — 17.35", "16.00 - 16.45 \n 16.50 - 17.35",
                    "6 пара", "17.45 — 19.20", "17.45 - 18.30 \n 18.35 - 19.20",
                    "7 пара", "19.25 — 21.00", "19.25 - 20.10 \n 20.15 - 21.00"
            };
        LinearLayout[] layer = new LinearLayout[ 21 ];
        for( int i = 0; i < layer.length; i++ )
        {
            layer[ i ] = createcell( rings[ i ] );
        }
        ArrayAdapter adapter = new ArrayAdapter<LinearLayout>(rootView.getContext(), R.layout.item, R.id.tvText, layer);
        GridView gvMain = (GridView) rootView.findViewById(R.id.gridView);
        gvMain.setAdapter(adapter);
        gvMain.setNumColumns(3);

        return rootView;
    }
    public LinearLayout createcell (String value)
    {
        LinearLayout layout = new LinearLayout(rootView.getContext());
        //layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        //TextView titleView = new TextView(rootView.getContext());
        //LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //titleView.setLayoutParams(lparams);
        //titleView.setTextAppearance(rootView.getContext(), android.R.attr.textAppearanceLarge);
        //titleView.setText( value );
        //layout.addView(titleView);

        return layout;
    }
}

What`s wrong with my code.

Screenshot of result in compiled app:

enter image description here

1

There are 1 answers

0
varren On BEST ANSWER

ArrayAdapter should have data and not views in it.

ArrayAdapter adapter = new ArrayAdapter<String>(..., rings);

You can look here for more info: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView If you really want to add LinearLayout in each cell of GridView, you have to create your custome Adapter and implement public View getView(int position, View convertView, ViewGroup parent)