0

I have a class with parameterized constructor and ArrayList as a parameter. The size of ArrayList is valid on constructor but on the onCreate method size is zero. How it's possible?

    public class ViewDetail extends AppCompatActivity {

    private static final String TAG = "val";
    final String POSITION_CLICKED="position";


    private ArrayList<DataModel> mData=new ArrayList<>();
    private Context mC;
    public ViewDetail( Context c,ArrayList<DataModel> data) {
        this.mData = data;
        this.mC = c;
        Log.v(TAG,"MDATA SIZE "+mData.size());
    }
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_detail);
    Log.v(TAG,"MDATA SIZE2 "+ mData.size());
        short pos = 0;
        try {
            pos = getIntent().getExtras().getShort(POSITION_CLICKED);
        } catch (Exception e) {
            e.printStackTrace();
        }
}}
Blue
  • 22,608
  • 7
  • 62
  • 92
amir dt
  • 83
  • 2
  • 9
  • I hope you are trying to pass an array list from one activity to another. Refer this https://stackoverflow.com/questions/21250339/how-to-pass-arraylistcustomeobject-from-one-activity-to-another. – Sivakumar S Jan 28 '18 at 06:41

1 Answers1

0
  public ViewDetail( Context c,ArrayList<DataModel> data) {
        this.mData = data;
        this.mC = c;
        Log.v(TAG,"MDATA SIZE "+mData.size());
    }

Wrong !!...Don't create constructor of that class which extends the Activity.

Instead of above you can pass that data via Intent

dharmendra
  • 7,835
  • 5
  • 38
  • 71