Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在表格布局中查找行和列索引?_Android_Android Tablelayout - Fatal编程技术网

Android 如何在表格布局中查找行和列索引?

Android 如何在表格布局中查找行和列索引?,android,android-tablelayout,Android,Android Tablelayout,我是以编程方式创建表布局的。当我单击表布局内的视图时,我想找出表的行索引和列索引,但无法获得解决方案。有人能帮我解决此问题吗 编码 private TableLayout createTableLayout(int rowCount, int columnCount,ArrayList<HashMap<String,String>> sheetdetails) { TableLayout.LayoutParams tableLayoutParams = new

我是以编程方式创建表布局的。当我单击表布局内的视图时,我想找出表的行索引和列索引,但无法获得解决方案。有人能帮我解决此问题吗

编码

private TableLayout createTableLayout(int rowCount, int columnCount,ArrayList<HashMap<String,String>> sheetdetails) {

    TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
    tableLayout = new TableLayout(this);
    tableLayout.setBackgroundColor(Color.TRANSPARENT);

    TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
    tableRowParams.width = 30;
    tableRowParams.height = 30;
    tableRowParams.setMargins(5,5,5,5);

    int img_id = 0;
    for (int i = 0; i < rowCount; i++) {
        // 3) create tableRow
        tableRow = new TableRow(this);
        tableRow.setBackgroundColor(Color.TRANSPARENT);

        for (int j= 0; j < columnCount; j++) {

            im = new ImageView(this);
            im.setImageResource(R.drawable.noseat1);
            for (int k=0;k<sheetdetails.size();k++){

                if(sheetdetails.get(k).get("Row").equalsIgnoreCase(String.valueOf(i))&&sheetdetails.get(k).get("Col").equalsIgnoreCase(String.valueOf(j))){
                    im.setImageResource(Integer.valueOf(sheetdetails.get(k).get("seat_img")));
                    break;
                }

            }
            im.setTag(img_id);
            im.setOnClickListener(this);
            tableRow.addView(im, tableRowParams);
            img_id++;
        }
        tableLayout.addView(tableRow,tableLayoutParams);
    }

    return tableLayout;
}

@Override
public void onClick(View v) {

    //here i want to display the row and column index like (0,0),(0,1)

}
private TableLayout createTableLayout(int-rowCount、int-columnCount、ArrayList-sheetdetails){
TableLayout.LayoutParams tableLayoutParams=新建TableLayout.LayoutParams();
tableLayout=新的tableLayout(本);
tableLayout.setBackgroundColor(颜色.透明);
TableRow.LayoutParams tableRowParams=新建TableRow.LayoutParams();
tableRowParams.width=30;
tableRowParams.height=30;
tableRowParams.setMargins(5,5,5,5);
int img_id=0;
对于(int i=0;i对于(int k=0;k,您可以通过下一种方法来实现:

添加新类

public static class TableData{
    public final int RowIndex;
    public final int ColumnIndex;
    public final int ImageId;

    public TableData(int rowIndex, int columnIndex, int imageId) {
        RowIndex = rowIndex;
        ColumnIndex = columnIndex;
        ImageId = imageId;
    }
}
更新您的
createTableLayout

im.setTag(new TableData(i, j, img_id));
单击更新您的
onClick

@Override
public void onClick(View v) {
    TableData tableData = (TableData) v.getTag();
    if (null != tableData) {
        Toast.makeText(this, "Clicked on " + tableData.RowIndex + ", " + tableData.ColumnIndex, Toast.LENGTH_LONG).show();
    }
}