Android ListView with Checkbox and all clickable

AndroidListviewCheckboxFocus

Android Problem Overview


> Possible Duplicate:
> Android: Binding data from a database to a CheckBox in a ListView?

i want to use a ListView with the items having following layout

------------------------- 
 [CB]    TV            TV
-------------------------

CB is a checkbox and TV is a Textview.

Now i've read somewhere that you can't have clickable items in a ListView. If you have some then you won't be able to click on the ListItems. But if I look at the GoogleMail app, this is possible. I can mark several messages with the checkbox (and then select an action for them) or i can click on the ListItem (or scroll with the dpad) to get to another screen. Does someone has code/example how this is possible?

Android Solutions


Solution 1 - Android

Set the CheckBox as focusable="false" in your XML layout. Otherwise it will steal click events from the list view.

Of course, if you do this, you need to manually handle marking the CheckBox as checked/unchecked if the list item is clicked instead of the CheckBox, but you probably want that anyway.

Solution 2 - Android

Set the listview adapter to "simple_list_item_multiple_choice"

ArrayAdapter<String> adapter;

List<String> values; // put values in this

//Put in listview
adapter = new ArrayAdapter<UserProfile>(
this,
android.R.layout.simple_list_item_multiple_choice, 
values);
setListAdapter(adapter);	

Solution 3 - Android

holder.checkbox.setTag(row_id);

and

holder.checkbox.setOnClickListener( new OnClickListener() {

				@Override
				public void onClick(View v) {
					CheckBox c = (CheckBox) v;

					int row_id = (Integer) v.getTag();

					checkboxes.put(row_id, c.isChecked());


				}
		});

Solution 4 - Android

this code works on my proyect and i can select the listview item and checkbox

<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:clickable="true" >

    <TextView
        android:id="@+id/label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4" />

    <CheckBox
        android:id="@+id/check"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:focusable="false"
        android:text="" >
    </CheckBox>

</LinearLayout>

Solution 5 - Android

Below code will help you:

public class DeckListAdapter extends BaseAdapter{

	  private LayoutInflater mInflater;
	    ArrayList<String> teams=new ArrayList<String>();
	    ArrayList<Integer> teamcolor=new ArrayList<Integer>();
	  
	   
	    public DeckListAdapter(Context context) {
	        // Cache the LayoutInflate to avoid asking for a new one each time.
	        mInflater = LayoutInflater.from(context);
	        
	        teams.add("Upload");
	        teams.add("Download");
	        teams.add("Device Browser");
	        teams.add("FTP Browser");
	        teams.add("Options");
	        
	        teamcolor.add(Color.WHITE);
	        teamcolor.add(Color.LTGRAY);
	        teamcolor.add(Color.WHITE);
	        teamcolor.add(Color.LTGRAY);
	        teamcolor.add(Color.WHITE);
	      
	       
	    }
	    

	   
	    public int getCount() {
	        return teams.size();
	    }

	    
	    public Object getItem(int position) {
	        return position;
	    }

	    
	    public long getItemId(int position) {
	        return position;
	    }

	   @Override
	    public View getView(final int position, View convertView, ViewGroup parent) {
	        final ViewHolder holder;
	        
	        
	        if (convertView == null) {
	            convertView = mInflater.inflate(R.layout.decklist, null);

	            holder = new ViewHolder();
	            holder.icon = (ImageView) convertView.findViewById(R.id.deckarrow);
	            holder.text = (TextView) convertView.findViewById(R.id.textname);
	            
             .......here you can use holder.text.setonclicklistner(new View.onclick.

	                    for each textview
	            
	            
	            System.out.println(holder.text.getText().toString());
	            
	            convertView.setTag(holder);
	        } else {
	            
	            holder = (ViewHolder) convertView.getTag();
	        }
	        
	         
	       
	         holder.text.setText(teams.get(position));
	         
	         if(position<teamcolor.size())
	         holder.text.setBackgroundColor(teamcolor.get(position));
	        
	         holder.icon.setImageResource(R.drawable.arraocha);
	         
	         
	         
	         
	    
	         
	       
	        return convertView;
	    }

	    class ViewHolder {
	    	ImageView icon;
	        TextView text;

	        
	        
	    }
}

Hope this helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncodie4711View Question on Stackoverflow
Solution 1 - AndroidPacificSkyView Answer on Stackoverflow
Solution 2 - AndroidErik BergstedtView Answer on Stackoverflow
Solution 3 - Androidmax4everView Answer on Stackoverflow
Solution 4 - AndroidBorjaView Answer on Stackoverflow
Solution 5 - AndroidUdaykiranView Answer on Stackoverflow