【问题标题】:Delete row dynamically from table layout in android从android中的表格布局中动态删除行
【发布时间】:2016-11-15 07:34:29
【问题描述】:

我有一个TableLayout,我为此动态添加了行。在每一行中,有 2 个元素,其中一个是 TextView,另一个是 Button。当我单击一行中存在的按钮时,该行应该被删除。这如何在 Android 中完成?如何查找 rowid 以及如何动态删除一行。谁能帮我解决这个问题。

【问题讨论】:

标签: android android-tablelayout


【解决方案1】:

onClick 按钮将为您提供单击的视图,即您的情况下的按钮。该按钮的父级是您要删除的行。从它的父级中删除该行将删除它。

如何实现此功能的示例:

    button.setOnClickListener(new OnClickListener()
    {
        @Override public void onClick(View v)
        {
            // row is your row, the parent of the clicked button
            View row = (View) v.getParent();
            // container contains all the rows, you could keep a variable somewhere else to the container which you can refer to here
            ViewGroup container = ((ViewGroup)row.getParent());
            // delete the row and invalidate your view so it gets redrawn
            container.removeView(row);
            container.invalidate();
        }
    });

【讨论】:

    【解决方案2】:

    您需要为动态添加行分配 ID,并使用它可以获取该特定行的值,或者您也可以删除单击行按钮的行。

    在 onCreate() 中:-

    addButton.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        mTable.addView(addRow(mInput.getText().toString()));
                    }
                });
    
     private TableRow addRow(String s) {
                TableRow tr = new TableRow(this);
                tr.setId(1000 + sCount);
                tr.setLayoutParams(new TableLayout.LayoutParams(
                        TableLayout.LayoutParams.FILL_PARENT,
                        TableLayout.LayoutParams.WRAP_CONTENT));
                TableRow.LayoutParams tlparams = new TableRow.LayoutParams(
                        TableRow.LayoutParams.WRAP_CONTENT,
                        TableRow.LayoutParams.WRAP_CONTENT);
                TextView textView = new TextView(this);
                textView.setLayoutParams(tlparams);
                textView.setText("New text: " + s);
                tr.addView(textView);
                TableRow.LayoutParams blparams = new TableRow.LayoutParams(
                        TableRow.LayoutParams.WRAP_CONTENT,
                        TableRow.LayoutParams.WRAP_CONTENT);
                final Button button = new Button(this);
                button.setLayoutParams(blparams);
                button.setText(" - ");
                button.setId(2000 + sCount);
                button.setOnClickListener(new OnClickListener(){
    
                    @Override
                    public void onClick(View v) {               
                        mTable.removeView(findViewById(v.getId() - 1000));
                    }           
                });
                tr.addView(button);
                sCount++;
                return tr;
        }
    

    表格布局:-

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    
        <LinearLayout
            android:id="@+id/parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
            <Button
                android:id="@+id/add"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
            <TableLayout
                android:id="@+id/table1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TableLayout>
        </LinearLayout>
    
    </ScrollView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-30
      • 2014-05-13
      • 2015-06-02
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多