【问题标题】:Add tablerow with textviews to tablelayout programmatically (Android)以编程方式将带有 textviews 的 tablerow 添加到 tablelayout (Android)
【发布时间】:2020-04-04 03:08:35
【问题描述】:
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TextView;

    import java.util.ArrayList;
    import java.util.List;


    public class ShowTableResults extends Activity {

        String TAG = "tag",
                username;

        DatabaseHandler dbHandler;

        TableLayout resultsTable;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            Log.d(TAG, "SHOWTABLERESULTS oncreate");

            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog_table_results);

            username = getIntent().getExtras().getString("username");

            resultsTable = (TableLayout) findViewById(R.id.dialog_table_results);

            dbHandler = new DatabaseHandler(this);

        }

        @Override
        protected void onResume() {

            Log.d(TAG, "SHOWTABLERESULTS onresume");

            super.onResume();


            for (int j = 0; j < objectNameList.size(); j++) {

                View table_row = (TableRow) findViewById(R.id.table_row);//this is null


                TextView[] textViewArray = new TextView[9];

                TextView t1 = new TextView(this);
                TextView t2 = new TextView(this);
                TextView t3 = new TextView(this);
                TextView t4 = new TextView(this);
                TextView t5 = new TextView(this);
                TextView t6 = new TextView(this);
                TextView t7 = new TextView(this);
                TextView t8 = new TextView(this);
                TextView t9 = new TextView(this);

                t1 = (TextView) table_row.findViewById(R.id.true_matching);
                t2 = (TextView) findViewById(R.id.half_matching);
                t3 = (TextView) findViewById(R.id.false_matching);

                t4 = (TextView) findViewById(R.id.true_counting);
                t5 = (TextView) findViewById(R.id.half_counting);
                t6 = (TextView) findViewById(R.id.false_counting);

                t7 = (TextView) findViewById(R.id.true_choosing);
                t8 = (TextView) findViewById(R.id.half_choosing);
                t9 = (TextView) findViewById(R.id.false_choosing);

                textViewArray[0] = t1;
                textViewArray[1] = t2;
                textViewArray[2] = t3;
                textViewArray[3] = t4;
                textViewArray[4] = t5;
                textViewArray[5] = t6;
                textViewArray[6] = t7;
                textViewArray[7] = t8;
                textViewArray[8] = t9;


                //behavor
                for (int i = 1; i < 4; i++) {

                    //response
                    for (int k = 1; k < 4; k++) {

                        Log.d(TAG,"k, , i, j "+k+i+j);

                        textViewArray[(i-1) * 3 + k-1 ].setText(0);

//when  TableRow table_row = (TableRow) findViewById(R.id.table_row),here is null
                    }

                }
                resultsTable.addView(table_row);
                resultsTable.requestLayout();
            }

        }
    }

这是我的表格布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    android:id="@+id/dialog_table_results"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:weightSum="1"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TableRow
        android:id="@+id/headerrow"

        >



        <TextView
            android:text=""
            android:layout_weight="0.25"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />
        <TextView
            android:text="@string/matching"

            android:layout_weight="0.25"
            android:layout_width="0dp"
            android:textAlignment="center"
            android:layout_height="wrap_content"/>

        <TextView
            android:text="@string/counting"
            android:layout_width="0dp"
            android:layout_height="wrap_content"  android:textAlignment="center"
            android:layout_weight="0.25"/>

        <TextView
            android:text="@string/choosing"
            android:layout_width="0dp"  android:textAlignment="center"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"/>

    </TableRow>

    <TableRow
        android:id="@+id/subheaderrow"
        >



        <TextView
            android:text=""
            android:layout_weight="0.25"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

        <TextView
            android:text="@string/trueanswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/halfanswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/wronganswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/trueanswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/halfanswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/wronganswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/trueanswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/halfanswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/wronganswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>


    </TableRow>





</TableLayout>

这是用于添加活动的表格行,我添加了仍在布局中的表格布局行,我猜这并不重要。

<TableRow xmlns:android="http://schemas.android.com/apk/res/android"

        android:id="@+id/table_row"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        >

        <TextView
            android:text=""
            android:layout_weight="0.25"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/object_name"
            />



        <TextView
            android:text="@string/trueanswer" android:layout_width="0dp" android:id="@+id/true_choosing"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

    <TextView
        android:text="@string/wronganswer" android:layout_width="0dp" android:id="@+id/half_choosing"
        android:layout_height="wrap_content"
        android:layout_weight="0.0833"/>

        <TextView

            android:text="@string/halfanswer" android:layout_width="0dp" android:id="@+id/false_choosing"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>



        <TextView
            android:text="@string/trueanswer" android:layout_width="0dp" android:id="@+id/true_matching"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

    <TextView
        android:text="@string/wronganswer" android:layout_width="0dp" android:id="@+id/half_matching"
        android:layout_height="wrap_content"
        android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/halfanswer" android:layout_width="0dp" android:id="@+id/false_matching"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>



        <TextView
            android:text="@string/trueanswer" android:layout_width="0dp" android:id="@+id/true_counting"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

    <TextView
        android:text="@string/wronganswer" android:layout_width="0dp" android:id="@+id/half_counting"
        android:layout_height="wrap_content"
        android:layout_weight="0.0833"/>

    <TextView
            android:text="@string/halfanswer" android:layout_width="0dp" android:id="@+id/false_counting"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>



    </TableRow>

我有一个活动。我想展示桌子。为此,我有一个带有 2 行的表格布局和一个带有 textviews 的表格布局,每个都有 id。

我想通过该 id 以编程方式添加文本视图,但我无法得到这个。

       TableRow table_row = (TableRow) findViewById(R.id.table_row)
 t1 = (TextView) findViewById(R.id.true_matching);
     textViewArray[(i-1) * 3 + k-1 ].setText(0);

这使 null

 t1 = (TextView) findViewById(R.id.true_matching); 

还有这个。我需要那个文本视图,我不想使用参数。不可能吗?我搜索过,但他们总是添加参数。

也不行

table_row = getLayoutInflater().inflate(R.layout.table_row, null);

【问题讨论】:

    标签: android


    【解决方案1】:

    您的“dialog_table_results”和“table_row”似乎在外部布局文件中, 要在你的活动中使用它们,你必须先像这样给它们充气

    resultsTable = getLayoutInflater().inflate(R.layout.dialog_table_results, null);
    table_row = getLayoutInflater().inflate(R.layout.table_row, null);
    

    对于您的 TextView,所有内容都必须像这样初始化(从您的 table_row)

    t1 = (TextView) table_row.findViewById(R.id.true_matching);
    

    最后,您必须像这样将膨胀的结果表添加到活动中的现有布局中

    containerLayout.addView(resultsTable);
    

    ps:响应为时已晚,但它可以帮助处于相同情况的人。

    【讨论】:

      猜你喜欢
      • 2011-11-08
      • 2012-12-17
      • 2011-07-04
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多