Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

ListFragment can't use @ItemClick #432

Description

@fly1tkg

For Example, below code didn't work in the ListFragment. @ItemClick annotation is not able to use in ListFragment?
I wan't to hook the ListView click ( android.R.id.list )

@ItemClick
public void listItemClick(Item item) {
    // do some
}

Activity

  1. pyricau commented on Dec 20, 2012

    @pyricau
    Contributor

    Hi @fly1ddid

    What do you exactly mean by "Didn't work" ? Is there any compile error, a runtime exception, or just nothing hapenning ?

    By the way, the method should be name listItemClicked(). You forgot the ed.

  2. fly1tkg commented on Dec 20, 2012

    @fly1tkg
    Author

    Hi @pyricau

    Sorry, I wrote this issue too brief.

    I'm using android.support.v4.app.ListFragment (using android-support-v4.jar).
    I wrote below code using @ItemClick, however nothing happenning.

    I think onItemClickedListener is overwritten because ListFragment has onListItemClicked method.

    package jp.fly1ddid.listfragmenttest;
    
    import android.os.Bundle;
    import android.support.v4.app.ListFragment;
    import android.widget.ArrayAdapter;
    import android.widget.Toast;
    
    import com.googlecode.androidannotations.annotations.EFragment;
    import com.googlecode.androidannotations.annotations.ItemClick;
    
    @EFragment
    public class MyListFragment extends ListFragment {
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            String[] listItemTexts = {
                    "test", "test", "test"
            };
            setListAdapter(new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, listItemTexts));
        }
    
        @ItemClick
        void listItemClicked(String string) {
            Toast.makeText(getActivity(), string, Toast.LENGTH_SHORT).show();
        }
    
        // @ItemClick(android.R.id.list)
        // void listItemClicked(String string) {
        // Toast.makeText(getActivity(), string, Toast.LENGTH_SHORT).show();
        // }
    
        // @Override
        // public void onListItemClick(ListView l, View v, int position, long id) {
        // Toast.makeText(getActivity(), (String) l.getAdapter().getItem(position),
        // Toast.LENGTH_SHORT).show();
        // }
    }

    and this is generated code.

    //
    // DO NOT EDIT THIS FILE, IT HAS BEEN GENERATED USING AndroidAnnotations.
    //
    
    
    package jp.fly1ddid.listfragmenttest;
    
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    
    public final class MyListFragment_
        extends MyListFragment
    {
    
        private View contentView_;
    
        private void init_(Bundle savedInstanceState) {
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            init_(savedInstanceState);
            super.onCreate(savedInstanceState);
        }
    
        private void afterSetContentView_() {
            {
                AdapterView<?> view = ((AdapterView<?> ) findViewById(android.R.id.list));
                if (view!= null) {
                    view.setOnItemClickListener(new OnItemClickListener() {
    
    
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            listItemClicked(((String) parent.getAdapter().getItem(position)));
                        }
    
                    }
                    );
                }
            }
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            contentView_ = super.onCreateView(inflater, container, savedInstanceState);
            afterSetContentView_();
            return contentView_;
        }
    
        public View findViewById(int id) {
            if (contentView_ == null) {
                return null;
            }
            return contentView_.findViewById(id);
        }
    
        public static MyListFragment_.FragmentBuilder_ builder() {
            return new MyListFragment_.FragmentBuilder_();
        }
    
        public static class FragmentBuilder_ {
    
            private Bundle args_;
    
            private FragmentBuilder_() {
                args_ = new Bundle();
            }
    
            public MyListFragment build() {
                MyListFragment_ fragment_ = new MyListFragment_();
                fragment_.setArguments(args_);
                return fragment_;
            }
    
        }
    
    }
  3. fly1tkg commented on Dec 20, 2012

    @fly1tkg
    Author

    Hi @pyricau

    I examine ListFragment source code.

    OnItemClickedListener for Listfragment#onListItemClick is set in onViewCreated().

    @ItemClick injects code in onCreateView().

    Documents says onViewCreated() Called immediately after onCreateView(), so onClickListener is overwritten.

    AA injects @ItemClick code not in onCreateView() but in onViewCreated().

  4. pyricau commented on Dec 22, 2012

    @pyricau
    Contributor

    Nice one @fly1ddid, you indeed found a bug. It seems that we should override onViewCreated(), and do the view injection / binding from there.

    Meanwhile, I guess you'll have to use the onListItemClicked method instead of using @ItemClick

  5. PerfectCarl commented on Dec 26, 2012

    @PerfectCarl
    Contributor

    I encountered that issue, too.
    Not sure it was a bug, though :)

    Nice report.

  6. atali commented on Jan 21, 2013

    @atali

    Hello,

    I face the same issue, how I could workaround it ?

  7. mathieuboniface commented on Jan 21, 2013

    @mathieuboniface
    Contributor

    Hi Atali,

    This issue as been fixed on the development branch, you can check out the latest snapshot built on this branch here :
    https://oss.sonatype.org/content/repositories/snapshots/org/androidannotations/androidannotations-bundle/3.0-SNAPSHOT/

  8. atali commented on Jan 21, 2013

    @atali

    Thanks Mathieu, I will give a try :-)
    Le 21 janv. 2013 10:19, "Mathieu Boniface" notifications@github.com a
    écrit :

    Hi Atali,

    This issue as been fixed on the development branch, you can check out the
    latest snapshot built on this branch here :

    https://oss.sonatype.org/content/repositories/snapshots/org/androidannotations/androidannotations-bundle/3.0-SNAPSHOT/

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/432#issuecomment-12489740.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions