Austin
Austin

Reputation: 4929

Removing a ContentView from List Activity

I have a simple list activity that shows all the file sin a certain directory. When I have at least one checked, I want a button to show up OVER the list at the bottom, I don't want it to be part of the list, because then you have to scroll all the way to the bottom to see the button. I have the button showing up, which is great, but I have a problem. After I add the button, I can't remove it. I want it removed when no files are checked. Here's my code

    if(size == 1) {
                Log.d("LIST", "Showing button!");

                Button button = new Button(selectFile.getApplicationContext());
                button.setId(556);
                button.setText("Click me to " + (SHRED_MODE ? "Shred!" : "Encrypt/Delete!"));
                   LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                   params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                 selectFile.addContentView(button, params);
            } else if (size == 0) {
                Log.d("LIST", "Removing Button!");
                View view = selectFile.findViewById(556);
                if(view == null) {
                    Log.d("List", "VIEW IS NULL");
                    return;
                }
                selectFile.getListView().removeView(view);
            }

I don't see anything like a removeContentView, and when I use removeView, I get this exception

06-12 13:38:33.371: ERROR/AndroidRuntime(13203): java.lang.UnsupportedOperationException: removeView(View) is not supported in AdapterView
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at android.widget.AdapterView.removeView(AdapterView.java:489)
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at austin.paid.productions.SelectFile$EfficientAdapter$1.onCheckedChanged(SelectFile.java:134)
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at android.widget.CompoundButton.setChecked(CompoundButton.java:124)
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at android.widget.CompoundButton.toggle(CompoundButton.java:86)
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at android.widget.CompoundButton.performClick(CompoundButton.java:98)
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at android.view.View$PerformClick.run(View.java:8816)
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at android.os.Handler.handleCallback(Handler.java:587)
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at android.os.Looper.loop(Looper.java:123)
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at java.lang.reflect.Method.invokeNative(Native Method)
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at java.lang.reflect.Method.invoke(Method.java:521)
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-12 13:38:33.371: ERROR/AndroidRuntime(13203):     at dalvik.system.NativeStart.main(Native Method)

Any Ideas?

Upvotes: 0

Views: 5426

Answers (2)

Jonathan Lidbeck
Jonathan Lidbeck

Reputation: 1614

As others have pointed out, there are better ways to accomplish your particular needs. However, for completeness, there is a way to do what you want.

If you added a View (button) to an Activity (selectFile) using this:

selectFile.addContentView(button, params);

...you would remove it like this:

ViewGroup parent = (ViewGroup) button.getParent(); parent.removeView(button);

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1007544

You are not only attempting to remove something from an AdapterView via removeView() -- which, as the stack trace indicates, is not allowed -- but that's not where the View is that you are trying to remove.

You added the View via:

selectFile.addContentView(button, params);

I presume that selectFile is an Activity instance, since that's the most likely candidate for what would respond to addContentView().

An Activity is not a ListView. selectFile is not selectFile.getListView(). You cannot remove a child you added to selectFile by trying to remove it from something that is not selectFile.

And, AFAICT, you cannot remove something you added via addContentView(), anyway.

So, please, follow @MisterSquonk's recommendation, and just have a fixed button at the bottom of the layout.

Also, do not call getApplicationContext() unless you specifically need the Application object. You do not need the Application object here. In fact, for GUI work, you almost never need the Application object. Please just pass the Activity to the Button constructor.

Upvotes: 2

Related Questions