我以编程方式创建了表格布局。我想找出表格的行和列索引,当我点击表格布局内的视图时,但是我无法得到解决方案。可以任何人知道帮助我来解决这个问题。
编码
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)
}
答案 0 :(得分:2)
你可以通过下一种方法做到这一点:
添加新课程
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();
}
}