线性布局到网格视图的单元格中

Linearlayout into cell of gridview

我在以编程方式创建 LinearLayout 包含到设计时创建 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;
    }
}

我的代码有什么问题。

已编译应用结果截图:

ArrayAdapter 中应该有数据而不是视图。

ArrayAdapter 适配器 = new ArrayAdapter<String>(..., rings);

您可以在这里查看更多信息:https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView 如果你真的想在 GridView 的每个单元格中添加 LinearLayout,你必须创建你的自定义适配器并实现 public View getView(int position, View convertView, ViewGroup parent)