I have programmatically added CheckedTextView to the linearlayout view. Please look at following code:
private LinearLayout linearLayout;
private CheckedTextView checkedtextview;
linearLayout = (LinearLayout) findViewById(R.id.statusView);
checkedtextview = new CheckedTextView(ScanStatus.this, null, android.R.attr.listChoiceIndicatorMultiple);
checkedtextview.setText(R.string.applications);
linearLayout.addView(checkedtextview);
Later in code I have to update the checkedtextview like below:
checkedtextview.setCheckMarkDrawable(getDrawable(R.mipmap.check1));
checkedtextview.setChecked(true);
checkedtextview.setTextColor(Color.GREEN);
linearLayout.addView(checkedtextview);
But this results in crash with following log:
D/AndroidRuntime(24818): Shutting down VM E/AndroidRuntime(24818): FATAL EXCEPTION: main E/AndroidRuntime(24818): Process: com.example.ashwini.timapp, PID: 24818 E/AndroidRuntime(24818): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Please suggest me how can I update the view?
You have two options. First, if you have a reference to your checkedtextview all the time - you can update it without calling addView:
checkedtextview.setCheckMarkDrawable(getDrawable(R.mipmap.check1));
checkedtextview.setChecked(true);
checkedtextview.setTextColor(Color.GREEN);.
In the second case use tip from @坚持远方 answer:
linearLayout.removeView(checkedtextview);
checkedtextview.setCheckMarkDrawable(getDrawable(R.mipmap.check1));
checkedtextview.setChecked(true);
checkedtextview.setTextColor(Color.GREEN);
linearLayout.addView(checkedtextview);