0

I have a custom listview which contains 2 textviews and 1 switch items.When I click switch, nothing happens.Listview onItemClick doesn't catch it, too.I searched the web and ı tried steps below,but nothing happened ,again.

1.I added android:focusable="false" definition to each listview custom layout items.

2.This code always returns false for isChecked().

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Switch calendarSwitch = (Switch)adapterView.findViewById(R.id.calendar_switch);
               Log.e("CalendarActivity", " --> " + calendarSwitch.isChecked());
            }
        });
  1. I couldn't manage to catch any "EventCatched" message on logs with this code.

`

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
          Switch calendarSwitch = (Switch) adapterView.findViewById(R.id.calendar_switch);
                    calendarSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                            Log.e("Switch","EventCatched");
                        }
                    });
                }
            });`

Thanks in advance, any tips will be very helpful.

M. Reza Nasirloo
  • 16,434
  • 2
  • 29
  • 41
ACrescendo
  • 46
  • 10
  • I think adapterView is right choice.Check this link:[https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html ] Anyway, I tried your code, problem is still going on. – ACrescendo Mar 31 '18 at 07:23
  • read this `View: The view within the AdapterView that was clicked (this will be a view provided by the adapter)` – AskNilesh Mar 31 '18 at 07:23
  • Thanks for your reponse so AdapterView refers to listview, and view refers to custom layout for listview.But problem still occurs. @Nilesh Rathod – ACrescendo Mar 31 '18 at 07:33
  • This link would be helpful if it had contained example of checked textviews in listview.@pskink – ACrescendo Mar 31 '18 at 07:37
  • @ACrescendo check this https://stackoverflow.com/questions/2538539/how-to-add-a-checkbox-in-a-listview – AskNilesh Mar 31 '18 at 07:45
  • @ACrescendo also check this https://stackoverflow.com/questions/34940938/use-custome-listview-with-textview-and-checkbox-with-single-selection-of-checkbo – AskNilesh Mar 31 '18 at 07:45
  • I don't get it why people still use `ListView` over `RecyclerView` – M. Reza Nasirloo Mar 31 '18 at 08:22
  • @M.RezaNasirloo because there are no concrete, ready-to-use implementations of `RecyclerView.Adapter`? – pskink Mar 31 '18 at 08:31
  • @pskink There are lots of generic implementation of `RecyclerView.Adapter`, you can search it – M. Reza Nasirloo Mar 31 '18 at 08:53
  • @M.RezaNasirloo are they in `recyclerview-v7` support library? i dont think so... thats why people still use `ListView` as they are lazy and dont want to write adapters from the scratch... for example, what generic implementation can do the same thing in `RecyclerView`: https://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/ ? – pskink Mar 31 '18 at 08:58
  • @pskink No they are not. And lots of useful projects like Retrofit and Glide are not in support library too. In case of laziness you are right – M. Reza Nasirloo Mar 31 '18 at 09:30
  • @NileshRathod ı checked your links, I have 3 keywords in my mind to investigate .My code concepts are same with these links except these keywords. (BaseAdapter,SimpleCursorAdapter,ViewHolder) – ACrescendo Mar 31 '18 at 09:44
  • @NileshRathod,when I click a switch inside a listview , why isn't listview OnItemClick callback triggered? – ACrescendo Mar 31 '18 at 10:18

0 Answers0