Linearlayout进入gridview的单元格

时间:2015-06-08 21:35:43

标签: java android gridview android-linearlayout

我遇到了以编程方式创建的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;
    }
}

我的代码出了什么问题。

已编译应用中的结果屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:0)

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

ArrayAdapter adapter = new ArrayAdapter&lt; String &gt;(..., ring );

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