0

I would like to be able to add a custom view class to a specific layout using layout_name.addView(CUSTOM VIEW CLASS INSTANCE), or add them to a list view.

I intend to add many of these views programmatically, and would like to be able to attain the values of edit texts/spinners etc for each view individually.

When I attempt to add this custom view nothing appears, I also receive this error the second time the view is added.

 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
            at android.view.ViewGroup.addViewInner(ViewGroup.java:3577)
            at android.view.ViewGroup.addView(ViewGroup.java:3430)
            at android.view.ViewGroup.addView(ViewGroup.java:3375)
            at android.view.ViewGroup.addView(ViewGroup.java:3351)
            at com.modup.fragment.CreateFragment.onClick(CreateFragment.java:192)
            at android.view.View.performClick(View.java:4442)
            at android.view.View$PerformClick.run(View.java:18473)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5105)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
            at dalvik.system.NativeStart.main(Native Method)

What can I do to add this WorkoutView with a specific layout, into a LinearLayout which is part of the original layout inflated for the View which is used by setContentView()?

UPDATE:

I am trying to insert this view, as many times as necessary into a layout which is housed into a parent layout which is inflated for the view to be setContentView().

R.id.create_fragment_layout (used in original setContentView())

R.id.workout_layout (inside the create_fragment_layout)

I want to insert custom view into this layout, and this custom view needs to have its own R.id.somename_layout with many elements inside.

WorkoutView (Custom Class)

public class WorkoutView extends LinearLayout implements View.OnClickListener {
    int res_id;
    public Spinner spinnerDifficulty, spinnerTime, spinnerMuscleGroup, spinnerSets, spinnerReps;
    public EditText etWorkoutName;
    public Button btnRemoveWorkout;

    String workoutName, muscleGroup, sets, reps;
    LayoutInflater inflater;

    public WorkoutView(Context context) {
        super(context);
    }

    public WorkoutView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public WorkoutView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void init() {
        inflate(getContext(), R.layout.single_workout_layout, this);

        spinnerDifficulty = (Spinner) findViewById(R.id.spinnerDifficulty);
        spinnerReps = (Spinner) findViewById(R.id.spinnerReps);
        spinnerSets = (Spinner) findViewById(R.id.spinnerSets);
        spinnerMuscleGroup = (Spinner) findViewById(R.id.spinnerWorkoutGroup);
        spinnerTime = (Spinner) findViewById(R.id.spinnerTime);
        etWorkoutName = (EditText) findViewById(R.id.etWorkoutName);
        btnRemoveWorkout = (Button) findViewById(R.id.btnRemoveWorkout);
        btnRemoveWorkout.setOnClickListener(this);
    }

    public String getReps() {
        reps = spinnerReps.getSelectedItem().toString();
        return reps;
    }

    public String getWorkoutName() {
        workoutName = etWorkoutName.getText().toString().trim();
        return workoutName;
    }

    public String getMuscleGroup() {
        muscleGroup = spinnerMuscleGroup.getSelectedItem().toString();
        return muscleGroup;
    }

    public String getSets() {
        sets = spinnerSets.getSelectedItem().toString();
        return sets;
    }

    public SingleWorkout getAll() {
        //to be designed
        return null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnRemoveWorkout:
                Log.e("DID THIS WORK?", "BUTTON PRESSED");
                break;
        }

    }
}

How I am creating the view

workoutView = new WorkoutView(getActivity());

How I am adding the view to a LinearLayout

workoutLayout.addView(workoutView);
Coova
  • 1,758
  • 4
  • 30
  • 56
  • java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. Mean workoutView already was added into another view. – cxphong Mar 03 '15 at 04:28

2 Answers2

0

1.For retaining the data of the individual view you need to assign them a unique id. You can generate unique id's at runtime refer this link

Android: View.setID(int id) programmatically - how to avoid ID conflicts? maintaining unique id's

2.And for your second problem i think you need to create another object of that view and add it to the workout layout

Community
  • 1
  • 1
Dominic D'Souza
  • 891
  • 1
  • 7
  • 16
0

You'r inflating your layout file in your custom class. So already you have added a view in that custom class and again here you'r doing same thing

workoutLayout.addView(workoutView);

So problem occurs. You need to change following.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    workoutView = new WorkoutView(getActivity());
    setContentView(workoutView);
Piyush
  • 23,959
  • 6
  • 36
  • 71
  • So there is no way to have a parent layout, and than insert separate views into that parent layout? I would like to maintain the original ContentView that was created, but within that content view there is a layout that I am looking to insert my WorkoutView class into, many times. – Coova Mar 03 '15 at 18:02