0

I am trying to show a dialog on the click of my table row.My table rows are dynamically generated, having 3 columns.1 ImageView and 2 TextViews.

My table rows are generated by this loop

for (i = 0; i < name.length; i++) 
{
    tr = new TableRow(this);

    ImageView iv = new ImageView(this);
    tr.setClickable(true);
    tr.setOnClickListener(this);
    try {
        url = new URL(image_url[i]);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    InputStream content = null;
    try {
        content = (InputStream)url.getContent();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Drawable d = Drawable.createFromStream(content , "src"); 
    iv.setImageDrawable(d);
    tr.addView(iv,new LayoutParams(150,150));
    tv2 = new TextView(this);
    tv3 = new TextView(this);

    tv2.setText(name[i]);
    tv3.setText(location[i]);

    tv2.setPadding(10, 0, 0, 0);

    tv3.setPadding(10, 0, 0, 0);
    tr.setPadding(0, 1, 0, 1);

    tr.addView(tv2); // add TextView to row.
    tr.addView(tv3); // add TextView to row.

    tl.addView(tr);
}

I have tried so many things to make the rows clickable,but nothing seems to work. The onclicklistner method calls the onClick() method that I have defined in my code but it is never called.Please let me know what is the way through which I can call a show dialog method on the click of a particular row.Thanks

dave.c
  • 10,910
  • 5
  • 39
  • 62
iamtheone
  • 105
  • 1
  • 8

1 Answers1

1

Well, I suspect you are not implementing View.OnClickListener interface in your class which you pass as 'this' to tr.setOnClickListener(this); You shouldn't be calling onClick yourself.

You should be doing

class MyActivity implements View.OnClickListener {

@Override
public void onClick(View view) {

 // your click event
}

//for loop with dynamically generated rows
public void createTable() {
 // your code with tr.setOnClickListener(this); 
}
Anirudh
  • 2,524
  • 1
  • 14
  • 14