0

我创建了一个 For 循环,可以创建一些动态的相关布局。我有一个包含一些元素的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
                     }
                 });
}

谢谢

4

3 回答 3

2

您的“i”变量不是最终的。

你需要在这里理解的东西:

当您的 onClick 方法调用时,它不会知道在此循环期间“i”的值是什么。您需要找到一种将“i”传递给 onClicklistener 的方法。

我的建议是,创建一个实现 OnClickListener 并接受 int 变量的新类。将此变量保存为类成员,然后在 OnClick 方法中使用它。

希望这可以帮助 :)

于 2013-10-14T10:37:25.517 回答
0

您为每个点击事件使用相同的变量 i。您应该使用不同的计数器变量。

于 2013-10-14T10:36:41.793 回答
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
                }
            });
        }
于 2013-10-14T11:28:19.650 回答