我正在使用以下代码填充horizontalscrollview:
for(int I=0; I<jsonArraySize; I++)
{
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.best_seller_layout, null);
TextView txtProductName = (TextView) v.findViewById(R.id.dashboard_bestSellerName);
txtProductName.setText(bestSellersList.get(I).getProductName());
TextView txtPrice = (TextView) v.findViewById(R.id.dashboard_bestSellerPrice);
txtPrice.setText(String.valueOf(bestSellersList.get(I).getProductUnitPrice()));
ImageView imgProductImage = (ImageView) v.findViewById(R.id.dashboard_bestSellerImage);
imgProductImage.setImageBitmap(bestSellersImageList.get(I));
insertPoint.add(findViewById(R.id.dashboard_mylinear));
((ViewGroup) insertPoint.get(I)).addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
final int K = I;
insertPoint.get(K).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(Dashboard.this,"The index is: " + K, Toast.LENGTH_SHORT).show();
}
});
}
在这里,我使用setOnClickListener()来烘烤每个视图的索引,但是我总是得到最后一个索引,而不是0,1,2 ..... etc。以下一行内容有些可疑:
insertPoint.add(findViewById(R.id.dashboard_mylinear));
........也许由于这个原因,所有的布局看起来都是一样的,我不确定。有任何想法吗?
我认为问题在于将onClickListener设置为列表项。将侦听器设置为各个视图可以解决此问题:
final int K = I;
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeToast.makeText(Dashboard.this, "Pressed Index: "+ K, Toast.LENGTH_SHORT).show();
}
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句