Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

contextmenu - Android - EditTexts in Gallery show strange behaviour when being (long)-clicked

my program is based on Google's Hello Gallery example:
http://developer.android.com/guide/tutorials/views/hello-gallery.html
Instead of using images, I create a bunch of EditTexts in the constructor.

My question is now: When I long click on an EditText, I want its Context Menu (with "select all", "copy" and so on) to be shown. I've tried setting an OnItemLongClickListener which calls the selected view via myGallery.getAdapter().getView(position, ...).showContextMenu(), but that runs into a StackOverflowError (that's btw the reason why I posted my question here - ok, that one was lame.):

08-13 16:02:36.062: ERROR/AndroidRuntime(3400): FATAL EXCEPTION: main
java.lang.StackOverflowError
 at android.widget.AdapterView.getPositionForView(AdapterView.java:581)
 at android.widget.Gallery.showContextMenuForChild(Gallery.java:1049)
 at android.view.View.showContextMenu(View.java:2520)
 at de.test.gallery2.Main$1.onItemLongClick(Main.java:51)
 at android.widget.Gallery.dispatchLongPress(Gallery.java:1074)
 at android.widget.Gallery.showContextMenuForChild(Gallery.java:1055)

I have also tried to registerForContextMenu() the Gallery, then the EditTexts and then both, but everything failed. Does anbody of you have a solution?

Btw, the Gallery shows some other strange behaviour: When the application starts, the first EditText is centered but can't be edited when i tap on it. But when I tap on the second one (which is not centered), I can edit that one without it being centered. When I center the second EditText, I can only edit the third one and so on. When I center the last one, focus appears to vanish entirely and nothing can be edited anymore.

I will probably marry you if you can help me. Any help is appreciated. And believe me - I did a lot of research before asking this question. Really.
Thanks a lot

m1ntf4n

EDIT

Here is the code of my Activity. Sorry for the double post, didn't take the possibility of editing into consideration.

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Gallery gallery = (Gallery) findViewById(R.id.gallery);
        gallery.setAdapter(new LocalAdapter(this));
        gallery.setSpacing(50);

        registerForContextMenu(gallery);

        //Register the EditViews for ContextMenu.
        for(int i = 0; i < gallery.getAdapter().getCount(); ++i) {
            registerForContextMenu(gallery.getAdapter().getView(i, null, null));
        }

        //This listener will cause a StackOverflowError.
        /*gallery.setOnItemLongClickListener(new Gallery.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> a, View v, int i, long l) {
                return gallery.getAdapter().getView(i, null, null).showContextMenu();
            }
        });*/
    }

    public class LocalAdapter extends BaseAdapter {
        private Context mContext;
        private EditText[] editText;

        public LocalAdapter(Context c) {
            mContext = c;
            editText = new EditText[5];
            for(int i = 0; i != editText.length; ++i) {
                editText[i] = new EditText(mContext);
                editText[i].setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                editText[i].setText("TEXT " + i);
                editText[i].setTextSize(30);
            }
        }
        @Override
        public int getCount() {
            return editText.length;
        }
        @Override
        public Object getItem(int position) {
            return position;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return editText[position];
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From Google's documentation:

public void registerForContextMenu (View view)

Registers a context menu to be shown for the given view (multiple views can show the context menu). This method will set the View.OnCreateContextMenuListener on the view to this activity, so onCreateContextMenu(ContextMenu, View, ContextMenuInfo) will be called when it is time to show the context menu.

As you can see from the documentation, onCreateContextMenu() will be called in Main before the context menu is shown. You will need to override this method to create your custom context menu.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...