在下面的代码中,我分别为两个表创建了一个点击监听器。如何为两者进行单击事件以比较它们的行 ID。因为在比较行之后,我必须在它们之间画一条线。
for (i = 0; i < data.getItem().size(); i++) {
final TableRow tr = new TableRow(this);
final TableRow tr1 = new TableRow(this);
tr.setPadding(2, 2, 2, 2);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.FILL_PARENT);
int leftMargin=0;
int topMargin=2;
int rightMargin=0;
int bottomMargin=1;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(tableRowParams);
tr1.setLayoutParams(tableRowParams);
match1[i]= new TextView(this);
match1[i].setId(i);
System.out.println(match1[i].getId());
match1[i].setText(data.getItem().get(i));
System.out.println(match1[i].getText().toString());
match1[i].setTextColor(Color.WHITE);
match1[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
match1[i].setTextSize(12);
match1[i].setPadding(10,0,0,0);
tr.addView(match1[i]);
Log.e("TAG", "ID: "+match1[i].getId());
final View row=t1.getChildAt(i);
row.setOnClickListener(new OnClickListener(){
public void onClick(View v){
int row_id=t1.indexOfChild(row);
Log.e("TAG ID for Table 1", "ID: "+row_id);
//Toast.makeText(getBaseContext(), row_id, Toast.LENGTH_SHORT).show();
}
});
t1.addView(tr, tableRowParams);
match2[i]= new TextView(this);
match2[i].setId(i);
match2[i].setText(data.getTarget().get(i));
System.out.println(match2[i].getText().toString());
match2[i].setTextColor(Color.WHITE);
match2[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
match2[i].setTextSize(12);
//match2[i].setPadding(10,0,0,0);
tr.addView(match2[i]);
Log.e("TAG ID For Tabel 2", "ID: "+match1[i].getId());
final View row1=t2.getChildAt(i);
row1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
int row_id=t2.indexOfChild(row1);
Log.e("TAG", "ID: "+row_id);
//Toast.makeText(getBaseContext(), row_id, Toast.LENGTH_SHORT).show();
}
});
t2.addView(tr1, tableRowParams);
/*if(match1[i].getId() == match2[i].getId()){
//draw.onDraw(canvas);
Toast.makeText(this, "Yipee", Toast.LENGTH_SHORT).show();
}else
Toast.makeText(this, ":(((((", Toast.LENGTH_SHORT).show();*/
}