【问题标题】:Handling Dynamically added Table Rows xml Layout in TableLayout Android在 TableLayout Android 中处理动态添加的表格行 xml 布局
【发布时间】:2015-07-02 14:38:05
【问题描述】:

我在片段中有一个TableLayout,代码如下:

<TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/projectsTableLayout"
            android:orientation="vertical"
            android:layout_margin="10dp">
</TableLayout>

这是TableLayout标签下方的AddMore按钮:

<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  style="?android:attr/buttonBarButtonStyle"
  android:id="@+id/add_more_projects_btn"
  android:text="@string/addmore"
  android:textColor="@color/colorAccent"
  android:layout_below="@+id/projectsTableLayout"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="20dp"
  android:layout_marginBottom="20dp"/>

我还使用以下代码在单独的 xml 文件中创建了 TableRow 布局:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <EditText
            android:id="@+id/project_name__ET"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="10dp"
            android:gravity="center_vertical"
            android:hint="@string/projectnametxt"
            android:inputType="text"
            android:paddingLeft="5dp"
            android:paddingRight="5dp" />

        <LinearLayout
            android:id="@+id/yearProjectTypeLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="05dp"
            android:orientation="horizontal"
            android:weightSum="1">

            <Spinner
                android:id="@+id/yearSpinner"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:drawSelectorOnTop="false"
                android:spinnerMode="dropdown" />

            <Spinner
                android:id="@+id/projectTypeSpinner"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:drawSelectorOnTop="false"
                android:spinnerMode="dropdown" />
        </LinearLayout>
    </LinearLayout>
</TableRow>

我想要实现的是当应用程序加载时我将动态添加行 5 次。

我想在 Add More 按钮点击时再添加 5 行。当用户在应用程序中前进时,我还必须将所有值发送到网络服务器。

我很困惑如何向表格行中的组件(如(EditTextSpinners)提供 ID,并在用户移动到下一个屏幕时检索它。

我想以这种方式进行编码,以便可以优化并轻松检索在单击添加更多按钮时添加到TableLayout 的行的所有数据。

如何为行组件检索值和设置 ID。

编辑 ::

@Rohit Heera 建议的代码修改

我写了以下代码:

        projectTableLayout = (TableLayout) rootView.findViewById(R.id.projectsTableLayout);
        projectTableLayout.setStretchAllColumns(true);

        yearAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item, Constants.YEAR_ARRAY);
        projectTypeAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item, Constants.PROJECT_TYPE_ARRAY);
        projectListData = new ArrayList<ProjectData>();

        for(int i = 0; i < 5 ; i++)
        {
            tableRowStartCount = i;
            TableRow projectItemRow = new TableRow(getActivity().getApplicationContext());
            LayoutInflater inflator = (LayoutInflater) getActivity().getSystemService(getActivity().getApplicationContext().LAYOUT_INFLATER_SERVICE);
            projectItemRow.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT,
                    TableLayout.LayoutParams.MATCH_PARENT, 1.0f));
            View projectRowView = inflator.inflate(R.layout.project_row_item,projectItemRow,false);


            projectNameET = (EditText) projectRowView.findViewById(R.id.project_name__ET);

            yearSpinner = (Spinner) projectRowView.findViewById(R.id.yearSpinner);
            yearSpinner.setAdapter(yearAdapter);

            projectTypeSpinner = (Spinner) projectRowView.findViewById(R.id.projectTypeSpinner);
            projectTypeSpinner.setAdapter(projectTypeAdapter);
            projectItemRow.addView(projectRowView);

            ProjectData projectObj = new ProjectData();
            projectListData.add(projectObj);

            projectTableLayout.addView(projectItemRow,tableRowStartCount);
        }

您能否告诉我如何在上述循环中实现事件监听器?

【问题讨论】:

  • 请上传一些截图更好地解释它

标签: android android-fragments android-tablelayout


【解决方案1】:
Use this code to add rows in table 
/**
     * this method add the dynamic table row and display the list .
     */
    public void displayList(List<Data> datalist) {
        int i = 0;
        TableLayout projectsTableLayout = (TableLayout) findViewById(R.id.projectsTableLayout);
        projectsTableLayout.removeAllViews();
        for (i = 0; i <= datalist.size(); i++) {

            TableRow singleTableRow = new TableRow(this);

            LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflator.inflate(R.layout.addattendeelistdata, null);

            RelativeLayout parentRelativeLayout = (RelativeLayout) view
                    .findViewById(R.id.comleteRL);

            EditText project_name__ET = (EditText) view
                    .findViewById(R.id.project_name__ET);
            Spinner yearSpinner = (Spinner) view
                    .findViewById(R.id.yearSpinner);
            Spinner projectTypeSpinner = (Spinner) view
                    .findViewById(R.id.projectTypeSpinner);


            singleTableRow.addView(view);

            /** Add row to TableLayout. */

            projectsTableLayout.addView(singleTableRow, i);

        }


    }

And for get Reference for each object you need to Create seperate class .
If you tell me on which event you can move to next class so i can help you better.

Thanks

【讨论】:

  • 为了获取每个对象的参考,您需要创建单独的类。如果你告诉我在哪个活动上你可以转到下一堂课,那么我可以更好地帮助你。
  • 我将在操作栏上的按钮单击事件上从此片段切换到下一个活动,因此我想从所有行及其组件中检索所有值,以便我可以将其提交到网络服务器。你能告诉我如何从所有行中检索值吗?
  • 您需要为三个值创建一个模型类,一个用于编辑文本,两个用于微调器,我将为您提供代码..
  • 如果您有任何解决方案,您也可以看看这个问题。 stackoverflow.com/questions/31114670/…
  • 是的,我可以建议对布局进行适当的更改。
【解决方案2】:
Like Edit Text you need to create separte class for spinner item listeners and store the value and main list automatically get all the new values.

/**
     * this method add the dynamic table row and display the list .
     */

     List<ProjectData> mainList=new List<ProjectData>();

     // calling function
     displayList(mainList);



    public void displayList(List<ProjectData> datalist) {
        int i = 0;
        TableLayout projectsTableLayout = (TableLayout) findViewById(R.id.projectsTableLayout);
        projectsTableLayout.removeAllViews();
        for (Iterator<ProjectData> it :datalist.hasNext();) {

            TableRow singleTableRow = new TableRow(this);

            LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflator.inflate(R.layout.addattendeelistdata, null);

            RelativeLayout parentRelativeLayout = (RelativeLayout) view
                    .findViewById(R.id.comleteRL);

            EditText project_name__ET = (EditText) view
                    .findViewById(R.id.project_name__ET);
            Spinner yearSpinner = (Spinner) view
                    .findViewById(R.id.yearSpinner);
            Spinner projectTypeSpinner = (Spinner) view
                    .findViewById(R.id.projectTypeSpinner);
            project_name__ET.addTextChangedListener(new ListenEditText(it.next()) );

            singleTableRow.addView(view);

            /** Add row to TableLayout. */

            projectsTableLayout.addView(singleTableRow, i);

        }


    }

    class ListenEditText implements TextWatcher
    {
        ProjectData data;
        ListenEditText(ProjectData data)
        {
            this.data=data;

        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            data.project_name=s.toString();

        }}


    ModelClass;

    class ProjectData
    {
    public String project_name;
    public String year;
    public String projectType;
    }

【讨论】:

  • 你能告诉我为什么在 for 循环中使用 Iterator 没有理解它的概念。您还可以为微调器提供单独的课程吗?并且函数 displayList 用于从行中检索值我正确吗?
  • 它是一个反射的概念,当你使用 Itetar 或内部列表中的变化直接反映在原始列表中,这就是为什么我们使用迭代器,当你按下按钮时,主列表获取所有更新的值。跨度>
  • 但为此我需要在每次动态创建新行时创建 projectdata 的实例对象并存储在 datalist 中?用户如何在微调器上选择值并编辑维护和存储以供检索的文本?
  • 是的,你想要的行数是列表的第六个是获取更新值的唯一方法
  • 还有一个疑问,当应用程序第一次创建数据列表的行时,即 Iterator 它:datalist.hasNext();里面没有值,那么 forloop 将如何工作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多