我试图在我的表行的点击上显示一个对话框。我的表行是动态生成的,有3列.1 ImageView和2 TextViews。
我的表行由此循环生成
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);
}
我已经尝试了很多东西来使行可点击,但似乎没有任何效果。 onclicklistner方法调用我在我的代码中定义的onClick()方法,但它永远不会被调用。请让我知道在点击特定行时我可以通过什么方式调用show dialog方法。谢谢
答案 0 :(得分:1)
好吧,我怀疑你没有在你的班级中实现View.OnClickListener
接口,你将其作为'this'传递给tr.setOnClickListener(this);
你不应该自己调用onClick。
你应该这样做
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);
}