【发布时间】: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 行。当用户在应用程序中前进时,我还必须将所有值发送到网络服务器。
我很困惑如何向表格行中的组件(如(EditText,Spinners)提供 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