1

The simplified onCreate code below effectively creates dynamically a matrix of ImageViews on the basis of data in a file.

I (think) a listener is attached to each ImageView item. However, when I try to get the id of an item, v.getId() always returns -1.

What am I doing wrong?

public void onCreate(Bundle savedInstanceState) 
{
  super.onCreate(savedInstanceState);       
  this.setContentView(R.layout.packlevel);
  Resources resources = this.getResources();

  /*  
    process file data

  */

  for (int i = 0; i < 24; i++)
  {
    if (i % 6 == 0)
    {
      row = new TableRow(this);
      tableLayout.addView(row);
    }

    ImageView iv = new ImageView(this);

    int resourceId = resources.getIdentifier(tileNames[tilesMatrix[i]], "drawable",
getPackageName());
    iv.setImageResource( resourceId );
    iv.setOnTouchListener(this);

    row.addView(iv);
  } 
}    

public boolean onTouch(View v, MotionEvent event) 
{
  int tileId = v.getId();
}
4

0 回答 0