简体   繁体   English

动态获取 android 表布局中行的 position

[英]Dynamically get position of row in android table layout

I have a table layout that gets dynamically filled with data the table contains 4 columns, 3 are text views and 1 is and image button我有一个动态填充数据的表格布局,表格包含 4 列,3 个是文本视图,1 个是图像按钮

<TableLayout
    android:id="@+id/tablePicklist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:stretchColumns="*">

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="code"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="desc"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="barcode"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="action"/>

    </TableRow>

</TableLayout>

on clicking the image button I want to get the position of the row of the table to access the row data I also do not want to set an id for the button as the rows will removed dynamically单击图像按钮时,我想获取表格行的 position 以访问行数据我也不想为按钮设置 ID,因为行将动态删除

I have tried to get row id by view id but it returns 1我试图通过视图 ID 获取行 ID,但它返回 1

ImageButton imgBtn = new ImageButton(context);
imgBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Log.e("table row", view.getId());
    }
});

also by trying the table row, it returns -1也通过尝试表格行,它返回 -1

ImageButton imgBtn = new ImageButton(context);
imgBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        TableRow tr = (TableRow) view.getParent();
        Log.e("table row", tr.getId());
    }
});

I think it is to do with the table layout but unsure of it我认为这与表格布局有关,但不确定

Thanks谢谢

Figured it out.弄清楚了。

got the solution from https://stackoverflow.com/a/34823284/10754488https://stackoverflow.com/a/34823284/10754488获得解决方案

ImageButton imgBtn = new ImageButton(context);
imgBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        TableRow tr = (TableRow) view.getParent();
        int index = tableLayout.indexOfChild(row);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM