Listview items with custom adapter no longer clickable

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Listview items with custom adapter no longer clickable

Postby devboy » Mon Dec 28, 2009 10:21 pm

I had an xml linear layout being used in a custom adapter for my list view. This was all working fine, until I edited the layout of the item being plugged into the adapter. I added a nested linear layout to add a checkbox to the end of the row.

This worked, however now none of the items are clickable. I have tried adding the tag 'android:clickable="true"' however it doesn't seem to work. The xml layout is found below ...

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <LinearLayout android:layout_width="wrap_content"
  2.  
  3.         android:layout_height="wrap_content"
  4.  
  5.         android:orientation="horizontal"
  6.  
  7.         android:clickable="true"
  8.  
  9.         xmlns:android="http://schemas.android.com/apk/res/android">
  10.  
  11.         <LinearLayout
  12.  
  13.                 android:orientation="vertical"
  14.  
  15.                 android:id="@+id/LinearLayoutSyncRow"
  16.  
  17.                 android:layout_width="fill_parent"
  18.  
  19.                 android:layout_height="wrap_content"
  20.  
  21.                 android:clickable="true"
  22.  
  23.                 xmlns:android="http://schemas.android.com/apk/res/android">    
  24.  
  25.                 <TextView
  26.  
  27.                         android:text="row 1"
  28.  
  29.                         android:id="@+id/schedule_name"
  30.  
  31.                         android:layout_width="fill_parent"
  32.  
  33.                         android:layout_height="wrap_content"
  34.  
  35.                         android:textStyle="bold"
  36.  
  37.                         android:textSize="16sp"/>              
  38.  
  39.                 <TextView
  40.  
  41.                         android:text="row 2"
  42.  
  43.                         android:id="@+id/schedule_time"
  44.  
  45.                         android:layout_width="fill_parent"
  46.  
  47.                         android:layout_height="wrap_content"/>         
  48.  
  49.                 <TextView
  50.  
  51.                         android:text="row 3"
  52.  
  53.                         android:id="@+id/repeat_days"
  54.  
  55.                         android:layout_width="fill_parent"
  56.  
  57.                         android:layout_height="wrap_content"
  58.  
  59.                         android:textSize="12dp"/>
  60.  
  61.         </LinearLayout>
  62.  
  63.         <LinearLayout
  64.  
  65.                 android:orientation="vertical"
  66.  
  67.                 android:id="@+id/LinearLayoutSyncRow"
  68.  
  69.                 android:layout_width="fill_parent"
  70.  
  71.                 android:layout_height="wrap_content"
  72.  
  73.                 android:layout_gravity="right"
  74.  
  75.                 xmlns:android="http://schemas.android.com/apk/res/android">    
  76.  
  77.                                 <CheckBox android:id="@+id/CheckBox02" android:layout_gravity="right" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
  78.  
  79. </LinearLayout>
  80.  
  81. </LinearLayout>
Parsed in 0.005 seconds, using GeSHi 1.0.8.4


Any help would be appreciated!
User avatar
devboy
Freshman
Freshman
 
Posts: 9
Joined: Mon Dec 28, 2009 10:12 pm

Top

Postby jwei512 » Mon Dec 28, 2009 11:34 pm

Hmm...

Well this may or may not be the issue, but something weird I'm seeing is that you have two LinearLayouts with the same android:id = LinearLayoutSyncRow. I imagine this would give you problems especially if you are doing something like findViewById(r.id.LinearLayoutSyncRow) and then setting some kind of onClickListener or something.

I would check that out first... try changing the id of one of them and see if that solves your problem.
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby devboy » Tue Dec 29, 2009 12:18 am

Unfortunately that didn't work ... and to confirm, if I used the following layout, all works fine:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <LinearLayout
  2.  
  3.         android:orientation="vertical"
  4.  
  5.         android:id="@+id/LinearLayoutSyncRow"
  6.  
  7.         android:layout_width="fill_parent"
  8.  
  9.         android:layout_height="wrap_content"
  10.  
  11.         xmlns:android="http://schemas.android.com/apk/res/android">    
  12.  
  13.         <TextView
  14.  
  15.                 android:text="row 1"
  16.  
  17.                 android:id="@+id/schedule_name"
  18.  
  19.                 android:layout_width="fill_parent"
  20.  
  21.                 android:layout_height="wrap_content"
  22.  
  23.                 android:textStyle="bold"
  24.  
  25.                 android:textSize="16sp"/>              
  26.  
  27.         <TextView
  28.  
  29.                 android:text="row 2"
  30.  
  31.                 android:id="@+id/schedule_time"
  32.  
  33.                 android:layout_width="fill_parent"
  34.  
  35.                 android:layout_height="wrap_content"/>         
  36.  
  37.         <TextView
  38.  
  39.                 android:text="row 3"
  40.  
  41.                 android:id="@+id/repeat_days"
  42.  
  43.                 android:layout_width="fill_parent"
  44.  
  45.                 android:layout_height="wrap_content"
  46.  
  47.                 android:textSize="12dp"/>
  48.  
  49. </LinearLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
User avatar
devboy
Freshman
Freshman
 
Posts: 9
Joined: Mon Dec 28, 2009 10:12 pm

Postby jwei512 » Tue Dec 29, 2009 12:26 am

Darn well the only other thing I can think of is removing those xmnls clauses... I know those are only required for the root element but I don't know if having them throughout the layout makes any difference so try:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <LinearLayout android:layout_width="wrap_content"
  3.  
  4.     android:layout_height="wrap_content"
  5.  
  6.     android:orientation="horizontal"
  7.  
  8.     android:clickable="true"
  9.  
  10.     xmlns:android="http://schemas.android.com/apk/res/android">
  11.  
  12.      <LinearLayout
  13.  
  14.          android:orientation="vertical"
  15.  
  16.          android:id="@+id/LinearLayoutSyncRow"
  17.  
  18.          android:layout_width="fill_parent"
  19.  
  20.          android:layout_height="wrap_content"
  21.  
  22.          android:clickable="true">  
  23.  
  24.           <TextView
  25.  
  26.               android:text="row 1"
  27.  
  28.               android:id="@+id/schedule_name"
  29.  
  30.               android:layout_width="fill_parent"
  31.  
  32.               android:layout_height="wrap_content"
  33.  
  34.               android:textStyle="bold"
  35.  
  36.               android:textSize="16sp"/>          
  37.  
  38.           <TextView
  39.  
  40.               android:text="row 2"
  41.  
  42.               android:id="@+id/schedule_time"
  43.  
  44.               android:layout_width="fill_parent"
  45.  
  46.               android:layout_height="wrap_content"/>        
  47.  
  48.           <TextView
  49.  
  50.               android:text="row 3"
  51.  
  52.               android:id="@+id/repeat_days"
  53.  
  54.               android:layout_width="fill_parent"
  55.  
  56.               android:layout_height="wrap_content"
  57.  
  58.               android:textSize="12dp"/>
  59.  
  60.      </LinearLayout>
  61.  
  62.      <LinearLayout
  63.  
  64.          android:orientation="vertical"
  65.  
  66.          android:id="@+id/LinearLayoutSyncRow2"
  67.  
  68.          android:layout_width="fill_parent"
  69.  
  70.          android:layout_height="wrap_content"
  71.  
  72.          android:layout_gravity="right" >  
  73.  
  74.           <CheckBox
  75.  
  76.                 android:id="@+id/CheckBox02"
  77.  
  78.                 android:layout_gravity="right"
  79.  
  80.                 android:layout_width="wrap_content"
  81.  
  82.                 android:layout_height="wrap_content">
  83.  
  84.           </CheckBox>
  85.  
  86.       </LinearLayout>
  87.  
  88. </LinearLayout>
  89.  
  90.  
Parsed in 0.006 seconds, using GeSHi 1.0.8.4


And if this doesn't work then try posting your JAVA code so that people know how you're setting your onClickListeners.
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby devboy » Tue Dec 29, 2009 12:46 am

No worky :(

I don't manually set onclick listeners for the listview, as I let the API handle it.

Here is the code to set my custom adapter into the listview:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 String[] from = new String[] { SyncSchedule.Columns.LABEL,
  2.  
  3.                                 SyncSchedule.Columns.START_HOUR,
  4.  
  5.                                 SyncSchedule.Columns.DAYS_OF_WEEK };
  6.  
  7.  
  8.  
  9.                 // and an array of the fields we want to bind those fields to (in this case just text1)
  10.  
  11.                 int[] to = new int[] { R.id.schedule_name,
  12.  
  13.                                 R.id.schedule_time,
  14.  
  15.                                 R.id.repeat_days };
  16.  
  17.                
  18.  
  19.         // Now create a sync schedule custom adapter and set it to display
  20.  
  21.                 mAdapter = new SyncScheduleDataAdapter(this,
  22.  
  23.                                 R.layout.sync_row, cursor, from, to);
  24.  
  25.                
  26.  
  27.         setListAdapter(mAdapter);
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


And here is my custom adapter:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public SyncScheduleDataAdapter(final Context context, int layout, Cursor cursor, String[] from, int[] to) {
  2.  
  3.                 super(context, layout, cursor, from, to);
  4.  
  5.                
  6.  
  7.                 this.mContext = context;
  8.  
  9.                 setViewBinder(new SyncScheduleDataViewBinder());
  10.  
  11.         }
  12.  
  13.        
  14.  
  15.         public class SyncScheduleDataViewBinder implements SimpleCursorAdapter.ViewBinder {
  16.  
  17.         public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
  18.  
  19.                         String time;
  20.  
  21.                 // If time, format appropriately
  22.  
  23.                 if (columnIndex == SyncSchedule.Columns.SYNC_SCHEDULE_START_HOUR_INDEX) {
  24.  
  25.                         time = SyncSchedule.formatTime(mContext,
  26.  
  27.                                         cursor.getInt(SyncSchedule.Columns.SYNC_SCHEDULE_START_HOUR_INDEX),
  28.  
  29.                                         cursor.getInt(SyncSchedule.Columns.SYNC_SCHEDULE_START_MINUTES_INDEX),
  30.  
  31.                                         new DaysOfWeek(cursor.getInt(SyncSchedule.Columns.SYNC_SCHEDULE_DAYS_OF_WEEK_INDEX)));
  32.  
  33.                        
  34.  
  35.                         // If periodical sync, append end time
  36.  
  37.                         SyncScheduleType syncType = SyncScheduleType.getByValue(cursor.getString(SyncSchedule.Columns.SYNC_SCHEDULE_SYNC_TYPE_INDEX));
  38.  
  39.                         if (syncType == SyncScheduleType.PERIODICAL) {
  40.  
  41.                         time += " - " + SyncSchedule.formatTime(mContext,
  42.  
  43.                                                 cursor.getInt(SyncSchedule.Columns.SYNC_SCHEDULE_END_HOUR_INDEX),
  44.  
  45.                                                 cursor.getInt(SyncSchedule.Columns.SYNC_SCHEDULE_END_MINUTES_INDEX),
  46.  
  47.                                                 new DaysOfWeek(cursor.getInt(SyncSchedule.Columns.SYNC_SCHEDULE_DAYS_OF_WEEK_INDEX)));
  48.  
  49.                         }
  50.  
  51.                         // Bind value then return true
  52.  
  53.                         ((TextView) view).setText(time);
  54.  
  55.                        
  56.  
  57.                         return true;
  58.  
  59.                 }
  60.  
  61.                
  62.  
  63.                 // If repeat days, format to string
  64.  
  65.                 if (columnIndex == SyncSchedule.Columns.SYNC_SCHEDULE_DAYS_OF_WEEK_INDEX) {
  66.  
  67.                         int repeatDays = cursor.getInt(SyncSchedule.Columns.SYNC_SCHEDULE_DAYS_OF_WEEK_INDEX);
  68.  
  69.                         // Bind value then return true
  70.  
  71.                         ((TextView) view).setText(SyncSchedule.getRepeatDaysString(mContext, true, repeatDays));
  72.  
  73.                        
  74.  
  75.                         return true;
  76.  
  77.                 }
  78.  
  79.  
  80.  
  81.                 // Let normal process bind value
  82.  
  83.             return false;
  84.  
  85.         }
  86.  
  87.         }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
User avatar
devboy
Freshman
Freshman
 
Posts: 9
Joined: Mon Dec 28, 2009 10:12 pm

Postby achie1266 » Tue Dec 29, 2009 1:01 am

hmm I heard a similar problem when using lists which have clickListeners within them. Look at this
http://www.anddev.org/viewtopic.php?p=3 ... ght=#31410.

Try setting up the click listener and log it out to see if its atleast calling the function.
try setting up click listener for the checkbox.

If that doesnot work post the related layout files and the code. I will try to recreate the problem and solve it if I can.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Top

Postby devboy » Tue Dec 29, 2009 2:04 am

Further playing around and if I set the focusable value of the checkbox to false, all is ok:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.  
  3.         android:layout_width="fill_parent"
  4.  
  5.    android:layout_height="wrap_content"
  6.  
  7.    android:orientation="horizontal"
  8.  
  9.    android:layout_gravity="right">
  10.  
  11.         <LinearLayout
  12.  
  13.          android:orientation="vertical"
  14.  
  15.          android:id="@+id/LinearLayoutSyncRow"
  16.  
  17.          android:layout_width="fill_parent"
  18.  
  19.          android:layout_height="wrap_content">  
  20.  
  21.           <TextView
  22.  
  23.               android:text="row 1"
  24.  
  25.               android:id="@+id/schedule_name"
  26.  
  27.               android:layout_width="fill_parent"
  28.  
  29.               android:layout_height="wrap_content"
  30.  
  31.               android:textStyle="bold"
  32.  
  33.               android:textSize="16sp"/>          
  34.  
  35.           <TextView
  36.  
  37.               android:text="row 2"
  38.  
  39.               android:id="@+id/schedule_time"
  40.  
  41.               android:layout_width="fill_parent"
  42.  
  43.               android:layout_height="wrap_content"/>        
  44.  
  45.           <TextView
  46.  
  47.               android:text="row 3"
  48.  
  49.               android:id="@+id/repeat_days"
  50.  
  51.               android:layout_width="fill_parent"
  52.  
  53.               android:layout_height="wrap_content"
  54.  
  55.               android:textSize="12dp"/>
  56.  
  57.     </LinearLayout>
  58.  
  59.         <CheckBox android:id="@+id/syncchecked"
  60.  
  61.                 android:focusable="false"
  62.  
  63.                 android:layout_width="fill_parent"
  64.  
  65.                 android:layout_height="60dip"
  66.  
  67.                 android:layout_gravity="right"/>
  68.  
  69. </LinearLayout>
Parsed in 0.005 seconds, using GeSHi 1.0.8.4


The only problem I have now is that the checkbox still doesn't align itself to the right side of the row ... is there anything obvious im doing wrong in the xml?
User avatar
devboy
Freshman
Freshman
 
Posts: 9
Joined: Mon Dec 28, 2009 10:12 pm

Postby achie1266 » Tue Dec 29, 2009 2:20 am

yes since in your linearlayout[LinearLayoutSyncRow] you have the layout_width to fill_parent, it is filling the screen and the checkbox also has the same.
So add the parameter android:layout_weight="1.0" to your linear_layout and change your checkbox's layout_width to wrap_content.
The final xml may look something like this.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.     android:layout_width="fill_parent"
  6.  
  7.    android:layout_height="wrap_content"
  8.  
  9.    android:orientation="horizontal"
  10.  
  11.    android:layout_gravity="right">
  12.  
  13.      <LinearLayout
  14.  
  15.          android:orientation="vertical"
  16.  
  17.          android:id="@+id/LinearLayoutSyncRow"
  18.  
  19.          android:layout_width="fill_parent"
  20.  
  21.          android:layout_height="wrap_content"
  22.  
  23.          android:layout_weight="1.0">  
  24.  
  25.           <TextView
  26.  
  27.               android:text="row 1"
  28.  
  29.               android:id="@+id/schedule_name"
  30.  
  31.               android:layout_width="fill_parent"
  32.  
  33.               android:layout_height="wrap_content"
  34.  
  35.               android:textStyle="bold"
  36.  
  37.               android:textSize="16sp"/>          
  38.  
  39.           <TextView
  40.  
  41.               android:text="row 2"
  42.  
  43.               android:id="@+id/schedule_time"
  44.  
  45.               android:layout_width="fill_parent"
  46.  
  47.               android:layout_height="wrap_content"/>        
  48.  
  49.           <TextView
  50.  
  51.               android:text="row 3"
  52.  
  53.               android:id="@+id/repeat_days"
  54.  
  55.               android:layout_width="fill_parent"
  56.  
  57.               android:layout_height="wrap_content"
  58.  
  59.               android:textSize="12dp"/>
  60.  
  61.     </LinearLayout>
  62.  
  63.      <CheckBox android:id="@+id/syncchecked"
  64.  
  65.          android:focusable="false"
  66.  
  67.          android:layout_width="wrap_content"
  68.  
  69.          android:layout_height="60dip"
  70.  
  71.          android:layout_gravity="right"/>
  72.  
  73. </LinearLayout>
Parsed in 0.005 seconds, using GeSHi 1.0.8.4
That should make the checkbok to only occupy as much width as required for it and the rest of the width will be occupied by the linear layout.
Last edited by achie1266 on Tue Dec 29, 2009 2:25 am, edited 1 time in total.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby achie1266 » Tue Dec 29, 2009 2:23 am

I have a question about the same thing.
Can you tell me after you finish this if
Q1) Clicking on any listItem[not within the checkbox] is toggling the checkbox or not
Q2) Clicking on any listItem[within the checkbox] is toggling the checkbox or not

thank you.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby devboy » Tue Dec 29, 2009 12:51 pm

I will test and get back to you ... in the meantime, I made those xml changes and the checkbox is still not moving to the right :(

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.  
  3.         android:layout_width="fill_parent"
  4.  
  5.    android:layout_height="wrap_content"
  6.  
  7.    android:orientation="horizontal"
  8.  
  9.    android:layout_gravity="right">
  10.  
  11.         <LinearLayout
  12.  
  13.          android:orientation="vertical"
  14.  
  15.          android:id="@+id/LinearLayoutSyncRow"
  16.  
  17.          android:layout_width="fill_parent"
  18.  
  19.          android:layout_height="wrap_content"
  20.  
  21.          android:layout_weight="1.0">  
  22.  
  23.           <TextView
  24.  
  25.               android:text="row 1"
  26.  
  27.               android:id="@+id/schedule_name"
  28.  
  29.               android:layout_width="fill_parent"
  30.  
  31.               android:layout_height="wrap_content"
  32.  
  33.               android:textStyle="bold"
  34.  
  35.               android:textSize="16sp"/>          
  36.  
  37.           <TextView
  38.  
  39.               android:text="row 2"
  40.  
  41.               android:id="@+id/schedule_time"
  42.  
  43.               android:layout_width="fill_parent"
  44.  
  45.               android:layout_height="wrap_content"/>        
  46.  
  47.           <TextView
  48.  
  49.               android:text="row 3"
  50.  
  51.               android:id="@+id/repeat_days"
  52.  
  53.               android:layout_width="fill_parent"
  54.  
  55.               android:layout_height="wrap_content"
  56.  
  57.               android:textSize="12dp"/>
  58.  
  59.     </LinearLayout>
  60.  
  61.         <CheckBox android:id="@+id/syncchecked"
  62.  
  63.                 android:focusable="false"
  64.  
  65.                 android:layout_width="wrap_content"
  66.  
  67.                 android:layout_height="60dip"
  68.  
  69.                 android:layout_gravity="right"/>
  70.  
  71. </LinearLayout>
Parsed in 0.005 seconds, using GeSHi 1.0.8.4
User avatar
devboy
Freshman
Freshman
 
Posts: 9
Joined: Mon Dec 28, 2009 10:12 pm

Postby achie1266 » Tue Dec 29, 2009 5:12 pm

hmm I am not sure why,
I used the same layout and it works fine for me. I tested it.
Try changing the layout_width of inner linear_layout to wrap_content
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  <LinearLayout
  2.  
  3.          android:orientation="vertical"
  4.  
  5.          android:id="@+id/LinearLayoutSyncRow"
  6.  
  7.          android:layout_width="wrap_content"
  8.  
  9.          android:layout_height="wrap_content"
  10.  
  11.          android:layout_weight="1.0">  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

are you changing something from the java code[on either the listView or these layouts]?
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby devboy » Fri Jan 01, 2010 10:06 pm

No, I'm not changing the layout programmatically...

I tested the checkboxes in my listview, and the oncheckchangedlistener isnt fired when i click the list view item or the checkbox item itself ...
User avatar
devboy
Freshman
Freshman
 
Posts: 9
Joined: Mon Dec 28, 2009 10:12 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 5 guests