![](/img/trans.png)
[英]Unable to get row id of dynamically added rows in table layout of android
[英]Dynamically get position of row in android table layout
我有一個動態填充數據的表格布局,表格包含 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>
單擊圖像按鈕時,我想獲取表格行的 position 以訪問行數據我也不想為按鈕設置 ID,因為行將動態刪除
我試圖通過視圖 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());
}
});
也通過嘗試表格行,它返回 -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());
}
});
我認為這與表格布局有關,但不確定
謝謝
弄清楚了。
從https://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.