Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnItemClickListener doesn't seem to get called #3

Closed
GoogleCodeExporter opened this issue Jan 12, 2016 · 15 comments
Closed

OnItemClickListener doesn't seem to get called #3

GoogleCodeExporter opened this issue Jan 12, 2016 · 15 comments

Comments

@GoogleCodeExporter
Copy link

I tried adding an OnItemClickListener to both the adapter and the 
SectionListView, but neither gets called when I click an item.

I modified the onCreate in the SectionListActivity sample to:

    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        arrayAdapter = new StandardArrayAdapter(this, R.id.example_text_view, exampleArray);
        sectionAdapter = new SectionListAdapter(getLayoutInflater(), arrayAdapter);
        sectionAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
              Log.i("test","adapter onclick called");
            }
          });
        final SectionListView listView = (SectionListView) findViewById(getResources().getIdentifier(
                "section_list_view", "id", this.getClass().getPackage().getName()));
        listView.setAdapter(sectionAdapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
              Log.i("test","view onclick called");
            }
          });
    }

I never get a log message though when I click an item in the list.

Original issue reported on code.google.com by jayki...@gmail.com on 5 Jul 2011 at 12:13

@GoogleCodeExporter
Copy link
Author

I am not sure but I guess you have clickable elements in the item itself. You 
need to implement onClickListeners in your item view - only when you have 
non-clickable Views in the item they will pass-through your click to list or 
item. Rather than using global on Cllick I prefer to set the listeners in 
adapter's getView on particular Items.

Original comment by ja...@potiuk.com on 5 Jul 2011 at 6:21

  • Changed state: Fixed

@GoogleCodeExporter
Copy link
Author

This is using your SectionList project only with the SectionListActivity 
modified as noted.   I unchecked the library project setting and ran in the 
emulator in debug, so I'm not sure what "you have clickable elements in the 
item itself" means since this is basically your code (wiht two onclick 
listeners added).  In my book, if you can handle something globally, that's 
almost always better (and probably more resource efficient) than duplicating 
the logic for every instance in a list.  Anyway, can you add working onclick 
handling to your sample activity to avoid confusion about what works and what 
doesn't.

Thanks much...

Original comment by jayki...@gmail.com on 5 Jul 2011 at 6:45

@GoogleCodeExporter
Copy link
Author

Please re-read my answer. It's not my code, it's how android behaves when 
handling cliks (correctly). In order to use the list you need to understand how 
list behaves and how clicks are handled. If on click is handled by, say, button 
it will never be passed to container on which the button is. This is how just 
about every GUI framework in the world works. In my example adapter for each 
item returns view which has two clickable elements tha handle all clicks. It 
would work exactly the same as in case of standard list.

So it is your job to supply the appropriate adapter that will return item view 
which is not clickable...
You do that -and then you can check further if onclick handling works.
And by the way. Please check in your book if there is a chapter about 
flexibility and applying the right tool to the right situation. Onitem click 
will only handle case when you click the whole item. In case you need to 
distinguish which element you clicked inside the item you need to do it inside 
the view. And both solutions are "global" in the sense they are defined in 
single place (adapter's get vie in my case) and applied to all items.

Original comment by ja...@potiuk.com on 5 Jul 2011 at 7:22

@GoogleCodeExporter
Copy link
Author

Good point.  I forgot that your sample included a checkbox on each row in the 
list, which I guess could be preventing the onItemClick from working properly. 
I'm pretty new to the android api, so much of the problem is likely due to my 
misinterpretation.

My adapter provided rows only contain a layout with a textview so nothing 
should be catching the clicks.  Before I found your code, I had an activity 
derived from ListActivity and that onItemClickListener fires fine with the same 
row layout.  

I was hoping you could update the sample to use a working onItemClickListener 
so I'd have a known working starting point to see where I've gone astray. If 
not, I guess I can set an onclick on each view returned by getView, though in 
my case that is a bit heavier than needed.  I appreciate your providing the 
component and the help...

Original comment by jayki...@gmail.com on 6 Jul 2011 at 6:25

@GoogleCodeExporter
Copy link
Author

Sure. Will do so within next day or so....

Original comment by ja...@potiuk.com on 6 Jul 2011 at 11:56

@GoogleCodeExporter
Copy link
Author

Hi,  Just checking if you were ever able to update your sample to show a 
working model of onItemClickListener.  This would be really helpful!  
All the best

Original comment by ho...@kajasoftware.com on 15 Sep 2011 at 5:44

@GoogleCodeExporter
Copy link
Author

Hi, in your documentation, you state "....and then register the adapter as 
listener in SectionListView."  Does this imply that I should be modifying code 
in the SectionListView.java file?  I just want to make sure that I understand 
your statement correctly.  Thank you! 

Original comment by hoond...@gmail.com on 15 Sep 2011 at 6:33

@GoogleCodeExporter
Copy link
Author

Here's what I ended up doing to get this to work:

    mAlphabetList = (SectionListView) findViewById(R.id.sectionListView1);
    mWrappedAdapter = new AlphaSectionAdapter(this,R.layout.section_item,list);
    adapter = new SectionListAdapter(getLayoutInflater(), mWrappedAdapter);
    mAlphabetList.setAdapter(adapter);
    mAlphabetList.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        adapter.onItemClick(parent, view, position, id);
      }
    });

Original comment by jayki...@gmail.com on 17 Sep 2011 at 6:11

@GoogleCodeExporter
Copy link
Author

Thanks very much.  I finally got mine working as well.  I finally understood 
what the docs were saying.

SectionListAdapter sa;
SectionListView secListView;

sa = new SectionListAdapter(getLayoutInflater(), aa);
secListView = (SectionListView) 
findViewById(getResources().getIdentifier("section_list_view", "id",
                        this.getClass().getPackage().getName()));
secListView.setAdapter(sa);

sa.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int _index,
                long arg3) {
                Log.d("DEBUG:ItemClick", String.valueOf(_index));
         }
          });

secListView.setOnItemClickListener(sa);

Original comment by hoond...@gmail.com on 21 Sep 2011 at 3:45

@GoogleCodeExporter
Copy link
Author

Issue 7 has been merged into this issue.

Original comment by jarek.po...@polidea.pl on 28 Jan 2012 at 10:01

@GoogleCodeExporter
Copy link
Author

Thanks for your coding first.  When I use your SectionListView in my app, I 
have found that the OnItemClickListener function is not triggered if image 
button attached in the list item.  However, the trigger will be fired again 
when I remove the image button.  Therefore, do i need to change activity as 
ListActivity instead of Activity.  Please clarify.

Original comment by jacky...@gmail.com on 16 Jul 2012 at 1:30

@GoogleCodeExporter
Copy link
Author

Please take a look at the previous answers. They exactly answer your problem.

Original comment by ja...@potiuk.com on 17 Jul 2012 at 7:25

@GoogleCodeExporter
Copy link
Author

You need put android:focusable="false" in your button element. Because the 
android not allow focusable elements on the listView element.

Original comment by santos.j...@gmail.com on 21 May 2013 at 10:19

@GoogleCodeExporter
Copy link
Author

I tried above solutions posted by "hoond..@gmail.com" and "jayki...@gmail.com", 
But neither worked for me. 
What I ended up is to manually applied onClick on the view, returned in getView 
method of the adapter class;
Like this:
@Override
        public View getView(final int position, final View convertView,
                final ViewGroup parent) {
            View view = convertView;
            if (view == null) {
                final LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = vi.inflate(R.layout.example_list_view, null);
            }
            final SectionListItem currentItem = items[position];
            if (currentItem != null) {
                final TextView textView = (TextView) view
                        .findViewById(R.id.example_text_view);
                final TextView textView2 = (TextView) view
                .findViewById(R.id.example_text_view2);
                if (textView != null) {
                    textView.setText(currentItem.item.toString());
                }
                if (textView2 != null) {
                    textView2.setText(currentItem.item.toString());
                }
                textView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Toast.makeText(SectionListActivity.this, "Item : " + position, Toast.LENGTH_SHORT).show();
                    }
                });
                textView2.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Toast.makeText(SectionListActivity.this, "Item : " + position, Toast.LENGTH_SHORT).show();
                    }
                });
            }

            return view;
        }
    }
Please suggest me if there is another better way of doing this.

Original comment by yunuslun...@gmail.com on 2 Sep 2013 at 11:56

@GoogleCodeExporter
Copy link
Author


Thanks yunuslun...@gmail.com! That helped alot. Just for completeness sake I am 
going to add my activity I finally got working without having to touch any of 
the other classes.

This will keep track of SECTION and ITEM clicks.
I know the section is not starting at 0, and the first section is not clickable.
But if you only need Items clickable, this should work.


SECTIONLISTACITIVTY:

/**
 * Example activity.
 */
public class SectionListActivity extends Activity implements 
AdapterView.OnItemClickListener {

    String TAG = this.getClass().getSimpleName();

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Log.d(TAG, "Section " + position + " clicked");
    }

    private class StandardArrayAdapter extends ArrayAdapter<SectionListItem> {

        private final SectionListItem[] items;

        public StandardArrayAdapter(final Context context, final int textViewResourceId,
                                    final SectionListItem[] items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(final int position, final View convertView, final ViewGroup parent) {
            View view = convertView;
            if (view == null) {
                final LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = vi.inflate(R.layout.adapter_slidesection_example_list_view, null);
            }
            final SectionListItem currentItem = items[position];
            if (currentItem != null) {
                final TextView textView = (TextView) view
                        .findViewById(R.id.example_text_view);

                if (textView != null) {
                    textView.setText(currentItem.item.toString());
                }

                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d("StandardArrayAdapter", "ListItem " + position + " clicked");
                    }
                });

            }

            return view;
        }
    }

    SectionListItem[] exampleArray = { // Comment to prevent re-format
            new SectionListItem("Test 1 - A", "A"), //
            new SectionListItem("Test 2 - A", "A"), //
            new SectionListItem("Test 3 - A", "A"), //
            new SectionListItem("Test 4 - A", "A"), //
            new SectionListItem("Test 5 - A", "A"), //
            new SectionListItem("Test 6 - B", "B"), //
            new SectionListItem("Test 7 - B", "B"), //
            new SectionListItem("Test 8 - B", "B"), //
            new SectionListItem("Test 9 - Long", "Long section"), //
            new SectionListItem("Test 10 - Long", "Long section"), //
            new SectionListItem("Test 11 - Long", "Long section"), //
            new SectionListItem("Test 12 - Long", "Long section"), //
            new SectionListItem("Test 13 - Long", "Long section"), //
            new SectionListItem("Test 14 - A again", "A"), //
            new SectionListItem("Test 15 - A again", "A"), //
            new SectionListItem("Test 16 - A again", "A"), //
            new SectionListItem("Test 17 - B again", "B"), //
            new SectionListItem("Test 18 - B again", "B"), //
            new SectionListItem("Test 19 - B again", "B"), //
            new SectionListItem("Test 20 - B again", "B"), //
            new SectionListItem("Test 21 - B again", "B"), //
            new SectionListItem("Test 22 - B again", "B"), //
            new SectionListItem("Test 23 - C", "C"), //
            new SectionListItem("Test 24 - C", "C"), //
            new SectionListItem("Test 25 - C", "C"), //
            new SectionListItem("Test 26 - D", "D"), //
            new SectionListItem("Test 27 - D", "D"), //
            new SectionListItem("Test 28 - D", "D"), //
            new SectionListItem("Test 28 - D", "D"), //
            new SectionListItem("Test 29 - D", "D"), //
    };

    private StandardArrayAdapter aa;

    private SectionListAdapter sectionListAdapter;

    private SectionListView sectionListView;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        //modified from the original
        //taken from https://code.google.com/p/android-section-list/issues/detail?id=3&can=1#makechanges from entry
        // #14 by yunuslun...@gmail.com
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sectionlistactivity_main);
        //like aa
        aa = new StandardArrayAdapter(this, R.id.example_text_view, exampleArray);

        sectionListAdapter = new SectionListAdapter(getLayoutInflater(), aa);

        sectionListView = (SectionListView) findViewById(R.id.section_list_view);
        sectionListView.setAdapter(sectionListAdapter);

        sectionListView.setOnItemClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.adapter_slidesection_test_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.standard_list:
                aa = new StandardArrayAdapter(this, R.id.example_text_view, exampleArray);
                sectionListAdapter = new SectionListAdapter(getLayoutInflater(), aa);
                sectionListView.setAdapter(sectionListAdapter);
                return true;
            case R.id.empty_list:
                aa = new StandardArrayAdapter(this, R.id.example_text_view, new SectionListItem[]{});
                sectionListAdapter = new SectionListAdapter(getLayoutInflater(), aa);
                sectionListView.setAdapter(sectionListAdapter);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}


sectionlistactivity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- for Adapters/SlideSectionList 
https://code.google.com/p/android-section-list/ -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/listView"
             android:orientation="vertical"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">

    <com.worksite360.android.Adapters.poliIdea.sectionedlist.SectionListView
        android:id="@+id/section_list_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </com.worksite360.android.Adapters.poliIdea.sectionedlist.SectionListView>
</FrameLayout>



adapter_section_example_list_view:

<?xml version="1.0" encoding="utf-8"?>
<!-- for Adapters/SlideSectionList 
https://code.google.com/p/android-section-list/ -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              style="@style/ListItemStyle"
              android:clickable="true">

    <TextView
        android:text="TextView"
        android:id="@+id/example_text_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="@style/ListTextViewStyle"></TextView>

    <!--<CheckBox-->
        <!--android:text=""-->
        <!--android:id="@+id/example_checkbox"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_weight="0"></CheckBox>-->

</LinearLayout>


adapter_section_test_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/standard_list"
          android:title="@string/standard_list" />
    <item android:id="@+id/empty_list"
          android:title="@string/empty_list" />
</menu>


If something doesnt work LET ME KNOW! (:

Original comment by jeffskiOSU@gmail.com on 16 Oct 2014 at 5:03

Attachments:

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

No branches or pull requests

1 participant