如何在GridLayout中获取特定子项的行和列

时间:2014-01-17 14:28:39

标签: android android-gridlayout

我绝对是Android编程中的生物。到目前为止,我已经做过这样的事了......

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.scorecard_fragment, container,
            false);

    gridLayout = (GridLayout) view.findViewById(R.id.GridLayout1);

    /*** Default first row */

    gridLayout.setColumnCount(col);
    gridLayout.setRowCount(row);

    if (col == 1) {
        defaultColumn();
    }

    else {

        extendedColumn("");

    }
    return view;
}

public void defaultColumn() {
    // Log.d(TAG, "Default col");

    int i;
    int k = 1;

    for (i = 0; i < 20; i++) {

        if (i == 0)

        {

            ImageView oImageView = new ImageView(getActivity());
            oImageView.setImageResource(R.drawable.ic_player);
            GridLayout.LayoutParams param = new   
  GridLayout.LayoutParams();

            param.setMargins(0, 0, 0, 0);

            gridLayout.addView(oImageView);

        } else if (i < 19) {

            TextView oTextView = new TextView(getActivity());

            oTextView.append("" + (k++) + "\n");
            GridLayout.LayoutParams param = new GridLayout.LayoutParams();
            param.setGravity(Gravity.CENTER);
            param.setMargins(15, 15, 15, 15);
            oTextView.setLayoutParams(param);
            gridLayout.addView(oTextView);

        } else {

            TextView oTextView = new TextView(getActivity());

            oTextView.append("\n");
            GridLayout.LayoutParams param = new GridLayout.LayoutParams();
            param.setGravity(Gravity.CENTER);
            param.setMargins(15, 15, 15, 15);
            oTextView.setLayoutParams(param);
            gridLayout.addView(oTextView);

        }

    }
    col++;

}

public void extendedColumn(String str) {

    Log.d("", "value c1" + col);

    int i;

    for (i = 0; i < 20; i++) {

        if (i == 0)

        {

            TextView oTextView = new TextView(getActivity());
            oTextView.setText(str);
            gridLayout.addView(oTextView);
            // oTextView.setBackgroundResource(R.drawable.border);
            oTextView.setTextSize(14);

        }

        else if (i < 19) {

            Log.d("Implement spinner", "sdfsdfsdf");

            final Spinner s1 = new Spinner(getActivity());

            final ArrayAdapter<CharSequence> adapter = ArrayAdapter
                    .createFromResource(getActivity(), R.array.score_array,
                            android.R.layout.simple_spinner_item);

            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s1.setAdapter(adapter);
            s1.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {

                    updateResult();

                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {

                }
            });

            gridLayout.addView(s1);

        }

        else {
            TextView oTextView = new TextView(getActivity());

            oTextView.append("\n");
            GridLayout.LayoutParams param = new GridLayout.LayoutParams();
            param.setGravity(Gravity.CENTER);
            param.setMargins(15, 15, 15, 15);
            oTextView.setLayoutParams(param);
            gridLayout.addView(oTextView);

        }
    }
    col++;

}

public void updateResult() {

    int j = gridLayout.getColumnCount();

    Log.d(" Column count", "" + j);

    int k = gridLayout.getRowCount();

    Log.d(" Row count", "" + k);

}

正如您所看到的,我正在尝试从微调器及其当前行和列值中获取值。我可以使用gridLayout.getColumnCount()和gridLayout.getRowCount()来做到这一点。但这总是给我总的列和行取决于我有多少用户。但是我想得到特定的行和列,无论我有多少用户,以便我可以通过知道哪个行和列被哪个用户选择来计算我的结果..敬请或建议将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

so that I can calculate my result by knowing which value has been selected

要仅了解所选值,只需实施onItemClick()方法,在内部您将获得所选值的positionviewparent ViewGroup等...