Elsunhoty
Elsunhoty

Reputation: 1627

Cant pass List To Custom View

Now I have this Custom View and I want to pass data from activity to this layout

public class ImageViewing extends LinearLayout {
    ArrayList<String> urlsArrayList = new ArrayList<>();
    public ImageViewing(Context context) {
        super(context);
        init();

    }

    public ImageViewing(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();

    }

    public ImageViewing(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }


    public ArrayList<String> getUrlsArrayList() {
        return urlsArrayList;
    }

    public void setUrlsArrayList(ArrayList<String> urlsArrayList) {
//        this.urlsArrayList.clear();
//        this.urlsArrayList.addAll(urlsArrayList);
        this.urlsArrayList   = urlsArrayList;
        invalidate();

    }

    private void init() {
        setOrientation(HORIZONTAL);
        if (urlsArrayList != null) {
            int size = urlsArrayList.size();
            AppLogger.log("Size",""+size);
            switch (size) {
                case 0:
                    break;
                case 1:
                    onePictureView();
                    break;
                case 2:
                    twoPictureView();
                    break;
                case 3:
                    threePictureView();

                    break;
                case 4:
                    fourPictureView();
                    break;
                default:
                    moreThanFourPictureView(size);
                    break;

            }
        } else {
        }
        invalidate();

    }}

and I want to pass Array List put list Is Always 0 size

I added it As view Like this

<com.tmoo7.carmasrclub.cutomView.ImageViewing
    android:id="@+id/viewImagesLayout"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

then In My Activity

  viewImagesLayout = findViewById(R.id.viewImagesLayout);
viewImagesLayout.setUrlsArrayList(model.getPostImageUrl());

can Any One help me and tell me what is wrong please can Any One help me and tell me what is wrong please can Any One help me and tell me what is wrong please

Upvotes: 0

Views: 266

Answers (2)

Vincent Chann
Vincent Chann

Reputation: 104

The problem comes from the order your methods chain up :
- constructor()
- init() { urlsArrayList = null }
- setUrlsArrayList(urlsArrayList)
-> setUrlsArrayList() is called after init()

If you want your methods to chain up in the right order you must call your "init()" in this lifecycle callback "onAttachedToWindow()"

public class ImageViewing extends LinearLayout {

   public ImageViewing(Context context, AttributeSet attrs) {
      super(context, attrs)
   }

   private void init() {...}

   public void setUrlsArrayList(ArrayList<String> urlsArrayList) {...}

   @Override
   protected void onAttachedToWindow() {
      init()
   }
}

Upvotes: 1

Patrick Chan
Patrick Chan

Reputation: 1029

put the switch case

        switch (size) {
            case 0:
                break;
            case 1:
                onePictureView();
                break;
            case 2:
                twoPictureView();
                break;
            case 3:
                threePictureView();

                break;
            case 4:
                fourPictureView();
                break;
            default:
                moreThanFourPictureView(size);
                break;

        }

into your "setUrlsArryList" method instead

public void setUrlsArrayList(ArrayList<String> urlsArrayList) {
    this.urlsArrayList   = urlsArrayList;
    // here
    invalidate();
    }

assuming that your onePictureView(), twoPictureView(), threePictureView() methods really change your view's appearance

Upvotes: 1

Related Questions