我创建了一个For循环,用于创建一些相对布局的dinamycally。我有一个带有一些元素的arrayList,称为“photosPathList”。此arrayList的大小用于定义最大循环数。 我正在编写的这段代码位于Android中一个活动的onResume()方法中。有一件事我不理解。 如果我在循环中记录i索引,它会正确增加,并且在每个循环中它需要+1值。 但是,如果我单击相对布局侦听器,则i索引始终是最后一个循环位置。因此,如果我创建4个相对布局并且我点击第一个布局,相对布局内的日志向我显示数字3.这不正确,因为它应该显示0数字。你同意吗? 那我在做错什么?
for (i=0; i<= photosPathList.size()-1; i++) {
//RELATIVE LAYOUT
RelativeLayout relativeLayout = new RelativeLayout(this); //create a new relative layout
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, //set main params about the width and height
RelativeLayout.LayoutParams.FILL_PARENT));
relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor)); //set background color
LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
relativeParams.setMargins(20, 20, 20, 0);
relativeLayout.setLayoutParams(relativeParams); //set declared params about layout to the relativeLayout
relativeLayout.requestLayout();
Log.i("index",""+i);
Log.i("current path",""+photosPathList.get(i).toString());
relativeLayout.setOnClickListener(new View.OnClickListener(){ //create a listener about the layout. When a user press a point inside the relative layout a new activity should be created
@Override
public void onClick(View v){
Intent photoDetailsActivity = new Intent(
getApplicationContext(),
PhotoDetails.class //assign the class for create a new intent
);
Log.i("index2",""+i);
photoDetailsActivity.putExtra("photoDetailsImagePath", photosPathList.get(i).toString());
photoDetailsActivity.putStringArrayListExtra("photosPathList" , photosPathList);
photoDetailsActivity.putStringArrayListExtra("formatPhotoList", formatPhotoList);
photoDetailsActivity.putStringArrayListExtra("numberCopiesPhotoList", numberCopiesPhotoList);
photoDetailsActivity.putStringArrayListExtra("pricePhotoList", pricePhotoList);
startActivity(photoDetailsActivity); //let's start the new activity
}
});
}
由于
答案 0 :(得分:2)
您的“i”变量不是最终的。
你需要在这里理解的东西:
当你的onClick方法调用时,它不知道在这个循环中“i”的值是多少。您需要找到一种方法将“i”传递给onClicklistener。
我的建议是,创建一个实现OnClickListener并接受int变量的新类。 将此变量保存为类成员,然后在OnClick方法中使用它。
希望这会有所帮助:)
答案 1 :(得分:0)
您为每个点击事件使用相同的变量i。您应该使用不同的计数器变量。
答案 2 :(得分:0)
// try this way
for (i=0; i<= photosPathList.size(); i++) {
//RELATIVE LAYOUT
RelativeLayout relativeLayout = new RelativeLayout(this); //create a new relative layout
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, //set main params about the width and height
RelativeLayout.LayoutParams.FILL_PARENT));
relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor)); //set background color
LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
relativeParams.setMargins(20, 20, 20, 0);
relativeLayout.setLayoutParams(relativeParams); //set declared params about layout to the relativeLayout
relativeLayout.requestLayout();
relativeLayout.setTag(i);
relativeLayout.setOnClickListener(new View.OnClickListener(){ //create a listener about the layout. When a user press a point inside the relative layout a new activity should be created
@Override
public void onClick(View v){
Intent photoDetailsActivity = new Intent(
getApplicationContext(),
PhotoDetails.class //assign the class for create a new intent
);
int index = (Integer)v.getTag();
Log.i("index2",""+index);
photoDetailsActivity.putExtra("photoDetailsImagePath", photosPathList.get(index).toString());
photoDetailsActivity.putStringArrayListExtra("photosPathList" , photosPathList);
photoDetailsActivity.putStringArrayListExtra("formatPhotoList", formatPhotoList);
photoDetailsActivity.putStringArrayListExtra("numberCopiesPhotoList", numberCopiesPhotoList);
photoDetailsActivity.putStringArrayListExtra("pricePhotoList", pricePhotoList);
startActivity(photoDetailsActivity); //let's start the new activity
}
});
}