Android 指定的子项已具有父项,单击事件
这是我的密码Android 指定的子项已具有父项,单击事件,android,Android,这是我的密码 for(int i=0;i<sitesList.getPdf().size();i++) { Bitmap bmp; URL url=null; InputStream is; ImageView iv=null; tr=new TableRow(this); tr.setLayoutParams(new LayoutParams( Lay
for(int i=0;i<sitesList.getPdf().size();i++)
{
Bitmap bmp;
URL url=null;
InputStream is;
ImageView iv=null;
tr=new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
for (int j=0;j<2;j++)
{
iv=new ImageView(this);
try
{
s1=sitesList.getThumbnail().get(count);
url = new URL(s1);
count++;
conn=(HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
bmp = BitmapFactory.decodeStream(is);
iv.setImageBitmap(bmp);
iv.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
iv.setId(ids);
int flag=iv.getId();
Log.v(".....Flag.....",+flag+"");
tr.addView(iv);
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
ids++;
Log.v(".....Counter...",count+"");
iv.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
int a=v.getId();
if(a==0)
{
Toast.makeText(getApplicationContext(), "0",Toast.LENGTH_SHORT).show();
}
else if(a==1)
{
Toast.makeText(getApplicationContext(), "1",Toast.LENGTH_SHORT).show();
}
else if(a==2)
{
Toast.makeText(getApplicationContext(), "2",Toast.LENGTH_SHORT).show();
}
else if(a==3)
{
Toast.makeText(getApplicationContext(), "3",Toast.LENGTH_SHORT).show();
}
else if(a==4)
{
Toast.makeText(getApplicationContext(), "4",Toast.LENGTH_SHORT).show();
}
else if(a==5)
{
Toast.makeText(getApplicationContext(), "5",Toast.LENGTH_SHORT).show();
}
}
});
}catch(Exception e)
{
e.printStackTrace();
}
}
//Log.v(".....Myids",ids+"");
//ids++;
}
for(int i=0;i在内部for循环中循环0,1次,即2次
在这个内部循环中,您两次将相同的tr(tablerow)添加到tl(tablelayout)中,因此它会给出指定的子级已存在父级警告
您没有正确地操作ids变量,因此只能获得0,1,2,iv.setId(ids)的toast;由于内部循环只重复两次,请检查ID变量。将堆栈跟踪放在上面。感谢回放,我可以使用tablerow数组吗?我使用相同的表行,因为我在一行中显示了两个图像…不要使用表行数组做一件事在表布局中添加表行tr,就在内部循环tl.addView(tr,new TableLayout.LayoutParams)之后(LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容));这将解决你的问题。是的,我做了同样的事情,它正在工作。谢谢。你知道如何从android emulator上的url添加pdf文件吗?你能给我一些链接吗?使用你的emulator的浏览器应用点击该url,下载将自动启动。别忘了让emulator支持SD卡。你能检查这个链接吗?