Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 动态添加TableRow并从edittext、viewtext检索/设置_Android_Layout_Dynamic_View_Row - Fatal编程技术网

Android 动态添加TableRow并从edittext、viewtext检索/设置

Android 动态添加TableRow并从edittext、viewtext检索/设置,android,layout,dynamic,view,row,Android,Layout,Dynamic,View,Row,我想将表格行动态添加到布局中,这些行将被添加到名为main\u ScrollView\u容器的RelativeLayout 我的问题是: 添加的行按添加顺序相互堆叠,而不是相互堆叠 如何检索添加的行,以便读取/写入添加的每一行的EditText输入和TextView输出 我的oncreate: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCo

我想将表格行动态添加到布局中,这些行将被添加到名为main\u ScrollView\u容器的RelativeLayout

我的问题是:

  • 添加的行按添加顺序相互堆叠,而不是相互堆叠
  • 如何检索添加的行,以便读取/写入添加的每一行的EditText输入和TextView输出
  • 我的oncreate:

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
    
        // the inflater
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // the item to inflate
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.main_ScrollView_Container);
        // the item to inflate with
    
        View tableRow = inflater.inflate(R.xml.my_row, relativeLayout, false);
        relativeLayout.addView(tableRow, 0);
    
        tableRow = inflater.inflate(R.xml.my_row, relativeLayout, false);
        relativeLayout.addView(tableRow, 1);
    
        tableRow = inflater.inflate(R.xml.my_row, relativeLayout, false);
        relativeLayout.addView(tableRow, 2);
    
        tableRow = inflater.inflate(R.xml.my_row, relativeLayout, false);
        relativeLayout.addView(tableRow, 3);
    
        // retrieve/set values to the EditText input
        // retrieve/set values to the TextView output
    }
    
    我得到了这个my_row.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp" >
    
        <EditText
            android:id="@+id/input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:ems="10"
            android:gravity="center"
            android:hint="@string/input"
            android:inputType="numberDecimal" />
    
        <TextView
            android:id="@+id/output"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:hint="@string/output"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>
    
    
    
    还有我的布局

    <ScrollView
        android:id="@+id/main_scroll_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <RelativeLayout
            android:id="@+id/main_ScrollView_Container"
            android:orientation="vertical" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
    
    
        </RelativeLayout>
    </ScrollView>
    

    首先,您可能应该使用
    R.layout
    而不是
    R.xml
    来组织和引用布局文件。仅在您的项目中就有许多类型的xml资源——最好将它们再分类

    其次,当调用
    relativeLayout.addView(tableRow,0)时
    实际上,您正在布局的第0个位置(顶部)添加
    tableRow
    。此外,由于要将这些行添加到
    RelativeLayout
    ,因此它们堆叠在一起也就不足为奇了。您可能希望使用垂直方向的
    线性布局
    ,它将负责从上到下垂直排列行

    第三,膨胀行视图后,可以按如下方式访问其子视图:

    View tableRow = inflater.inflate(R.xml.my_row, relativeLayout, false);
    EditText inputBox = (EditText) tableRow.findViewById(R.id.input);
    TextView outputBox = (TextView) tableRow.findViewById(R.id.output);
    

    记住,你可以在任何
    视图上调用
    findViewById
    来访问它的子视图,只要它们有ID。

    Nice,我会在你的答案中选择1和2。关于我的第二个问题,我不想在创建它们之后立即访问/编辑它们,我想以后再这样做,我想我需要将每个动态添加的视图保存在一个列表中,以便以后访问它们?由于每一行都有一个名称:@+id/tableRow和相同的EditText和TextView已经设置了id,那么我想我将始终获得androids内部列表中第一个id的第一个id的视图。