android - 向表布局中的行添加数据

标签 android android-tablelayout

我正在尝试创建一种 EPG 布局,我引用了这个 ( Print out ArrayList content in Android TableLayout ) 我的问题是当我尝试添加行时,它们被添加为新行但我需要将这些值添加到现有行中。 这是我的 XML 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >



    <TableLayout
        android:id="@+id/myTableLayout"
        android:layout_width="match_parent"
        android:layout_height="354dp"
        android:background="#6495ED"
        android:stretchColumns="*" >


     <TableRow
            android:id="@+id/tablerow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

             <TextView
                 android:id="@+id/textView1"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"

                 android:text="Some Text " 
                />

             <TextView
             android:id="@+id/textView2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"

             android:text="Some Text"
              />

             <TextView
             android:id="@+id/textView3"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"

             android:text="Some Text" />



        </TableRow>
</TableLayout>

我的java文件是

TableLayout table = (TableLayout) findViewById(R.id.myTableLayout);
            for(int i=0;i<channel_arraylist.size();i++)
            {

                TableRow tableRow2=new TableRow(VideoDatabase1.this);
                localchannel1= channel_arraylist.get(i);

                  TextView tvid=new TextView(VideoDatabase1.this);
                  tvid.setText(""+Integer.toString(localchannel1.getID()));
             tableRow2.addView(tvid);

            table.addView(tableRow2);

最佳答案

我对你问题的回答。

    private ArrayList<String> data = new ArrayList<String>();
    private ArrayList<String> data1 = new ArrayList<String>();
    private TableLayout table;    

    data.add("Samsung");
    data.add("Apple");
    data.add("Pixle");
    data.add("Vivo");
    data1.add(" $109");
    data1.add(" $200");
    data1.add(" $399");
    data1.add(" $290");        


    table = (TableLayout)findViewById(R.id.mytable);

    for(int i=0;i<data.size();i++)
    {
        TableRow row=new TableRow(this);
        String phone = data.get(i);
        String amount = data1.get(i);
        TextView tv1=new TextView(this);
        tv1.setText(phone);
        TextView tv2=new TextView(this);
        tv2.setText(amount);
        row.addView(tv1);
        row.addView(tv2);
        table.addView(row);
    }

关于android - 向表布局中的行添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18143598/

相关文章:

安卓选框不工作

Android ViewPager 像笔记本一样滑动页面更改

android - 如何从 Android 的 Gallery 应用程序中隐藏一些图像(由我的应用程序保存在 sdcard 上)?

android - 不能在两个不同的 View 上使用相同的 BindingAdapter

android - 以编程方式在表格布局中的按钮之间留出空间

java - TableRow 在 TableLayout 中返回空值?

缺少 Android Studio SDK

java - 我无法从 Activity 中填充 fragment 中的回收 View

android - ScrollView 开始变低并裁剪内容

Android 跨页面传播 TableLayout