Multiple CheckBoxes in a ListView Row

Put your problem here if it does not fit any of the other categories.

Multiple CheckBoxes in a ListView Row

Postby mo5ito » Fri Mar 11, 2011 9:03 pm

i Know it seems bizzare ,but how can i insert for exemple 2 checkBoxes for every item in a list View ? :roll:

setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked
,items));
it shows only one checkBox
mo5ito
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Mar 09, 2011 9:33 am

Top

Re: Multiple CheckBoxes in a ListView Row

Postby raj_J2ME » Mon Mar 14, 2011 7:29 am

mo5ito wrote:i Know it seems bizzare ,but how can i insert for exemple 2 checkBoxes for every item in a list View ? :roll:

setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked
,items));
it shows only one checkBox

I think that one check box can serves the objective for the list item selection. At the one time either you would like to select the item or you will not, then what is need for putting the two checkboxes?
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: Multiple CheckBoxes in a ListView Row

Postby ruelblapid » Mon Mar 14, 2011 9:26 am

Hi mo5ito,

Yes this is possible but it needs additional work :)
I developed a simple application hope this will help or give you an idea. just let me know if you have questions.
please see screenshot.

Iterator Xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.  xmlns:android="http://schemas.android.com/apk/res/android"
  4.  android:layout_width="fill_parent"
  5.  android:layout_height="fill_parent"
  6. >  
  7.   <ListView
  8.  android:id="@+id/list_item_iterator"
  9.  android:layout_width="fill_parent"
  10.  android:layout_height="wrap_content"
  11.  android:layout_gravity="center_vertical">
  12.   </ListView>  
  13.  </LinearLayout>  
  14.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Items XML
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:layout_width="wrap_content"
  4.    android:layout_height="wrap_content"
  5.    android:padding="2dip"
  6.    android:id="@+id/item">
  7.    
  8.     <ImageView
  9.        android:id="@+id/image"
  10.        android:src="@drawable/icon"
  11.        android:layout_width="80dp"
  12.        android:layout_height="80dp"
  13.        android:layout_marginRight="5dip"/>
  14.  
  15.     <RelativeLayout
  16.       android:layout_width="fill_parent"
  17.       android:layout_height="wrap_content"
  18.       android:layout_toRightOf="@id/image"
  19.       android:layout_centerVertical="true"
  20.       android:orientation="vertical">
  21.     <TextView
  22.        android:id="@+id/question"
  23.        android:layout_width="fill_parent"
  24.        android:layout_height="wrap_content"      
  25.        android:textSize="12dip" android:text="This is a test question?"
  26.        android:textStyle="bold"
  27.        
  28.         />  
  29.       <CheckBox
  30.          android:id="@+id/chkYes"
  31.          android:layout_below="@id/question"
  32.          android:text="Yes"
  33.          android:layout_width="wrap_content"
  34.          android:layout_height="wrap_content"/>
  35.       <CheckBox
  36.          android:id="@+id/chkNo"
  37.          android:layout_below="@id/question"
  38.          android:layout_toRightOf="@id/chkYes"
  39.          android:text="No"
  40.          android:layout_width="wrap_content"
  41.          android:layout_height="wrap_content"/>
  42.          
  43.     </RelativeLayout>
  44. </RelativeLayout>  
  45.  
Parsed in 0.005 seconds, using GeSHi 1.0.8.4


Manifest XML

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.      package="com.test"
  4.      android:versionCode="1"
  5.      android:versionName="1.0">
  6.     <uses-sdk android:minSdkVersion="4" />
  7.  
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  9.         <activity android:name=".MultipleCheckBox"
  10.                  android:label="@string/app_name">
  11.             <intent-filter>
  12.                 <action android:name="android.intent.action.MAIN" />
  13.                 <category android:name="android.intent.category.LAUNCHER" />
  14.             </intent-filter>
  15.         </activity>            
  16.     </application>
  17. </manifest>
  18.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Question Model
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.test;
  2.  
  3. import android.graphics.Bitmap;
  4.  
  5. public class QuestionModel {
  6.         private Bitmap icon = null;
  7.         private String question = "";
  8.         private boolean chkYes = false;
  9.         private boolean chkNo = false;
  10.        
  11.         public QuestionModel(Bitmap icon, String question, boolean chkYes,boolean chkNo)
  12.         {
  13.                 this.icon = icon;
  14.                 this.question = question;
  15.                 this.chkYes = chkYes;
  16.                 this.chkNo = chkNo;
  17.         }
  18.        
  19.         public Bitmap getImage()
  20.         {
  21.                 return this.icon;
  22.         }
  23.        
  24.         public String getQuestion()
  25.         {
  26.                 return this.question;
  27.         }
  28.        
  29.         public void setYes(boolean state)
  30.         {
  31.                 this.chkYes = state;
  32.         }
  33.        
  34.         public void setNo(boolean state)
  35.         {
  36.                 this.chkNo = state;
  37.         }
  38.        
  39.         public boolean getYes()
  40.         {
  41.                 return this.chkYes;
  42.         }
  43.        
  44.         public boolean getNo()
  45.         {
  46.                 return this.chkNo;
  47.         }
  48.        
  49. }
  50.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


This is the Activity Code

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.test;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import android.app.Activity;
  7. import android.content.Context;
  8. import android.graphics.BitmapFactory;
  9. import android.graphics.Color;
  10. import android.os.Bundle;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.AdapterView;
  15. import android.widget.BaseAdapter;
  16. import android.widget.CheckBox;
  17. import android.widget.CompoundButton;
  18. import android.widget.ImageView;
  19. import android.widget.ListView;
  20. import android.widget.RelativeLayout;
  21. import android.widget.TextView;
  22. import android.widget.Toast;
  23. import android.widget.AdapterView.OnItemClickListener;
  24. import android.widget.CompoundButton.OnCheckedChangeListener;
  25.  
  26. public class MultipleCheckBox extends Activity{
  27.         private List<QuestionModel> items = null;
  28.         @Override
  29.         protected void onCreate(Bundle savedInstanceState) {
  30.                 // TODO Auto-generated method stub
  31.                 super.onCreate(savedInstanceState);
  32.                 setContentView(R.layout.iterator);
  33.                 ListView lvQuestions = (ListView)this.findViewById(R.id.list_item_iterator);
  34.                 loadData();
  35.                 QuestionaireAdapter questions = new QuestionaireAdapter();
  36.                 lvQuestions.setAdapter(questions);
  37.                 lvQuestions.setOnItemClickListener(new OnItemClickListener(){
  38.  
  39.                         @Override
  40.                         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  41.                                         long arg3) {
  42.                                 //Your code logic here...
  43.                         }
  44.                        
  45.                 });
  46.         }
  47.        
  48.         private void loadData()
  49.         {
  50.                 items = new ArrayList<QuestionModel>();
  51.                 for(int i = 1; i <= 5; i++)
  52.                 {
  53.                         items.add(new QuestionModel(BitmapFactory.decodeResource(getResources(), R.drawable.icon),"Test Question " + i,false,false));
  54.                 }
  55.         }
  56.        
  57.         private class QuestionaireAdapter extends BaseAdapter
  58.         {      
  59.                 private LayoutInflater inflater =null;
  60.                 public QuestionaireAdapter()
  61.                 {
  62.                         this.inflater = MultipleCheckBox.this.getLayoutInflater();                     
  63.                 }
  64.                 @Override
  65.                 public int getCount() {                
  66.                         return items.size();
  67.                 }
  68.  
  69.                 @Override
  70.                 public Object getItem(int position) {
  71.                         return items.get(position);
  72.                 }
  73.  
  74.                 @Override
  75.                 public long getItemId(int position) {          
  76.                         return position;
  77.                 }
  78.  
  79.                 @Override
  80.                 public View getView(final int position, View convertView, ViewGroup parent) {
  81.                         convertView = inflater.inflate(R.layout.items, null);
  82.                         /*
  83.                         You can also use this to have alternate list item color
  84.                         RelativeLayout itemQuestion = null;
  85.                         if(position%2 == 0)
  86.                         {
  87.                                 itemQuestion = (RelativeLayout)convertView.findViewById(R.id.item);
  88.                                 itemQuestion.setBackgroundColor(Color.CYAN);
  89.                         }else{
  90.                                 itemQuestion = (RelativeLayout)convertView.findViewById(R.id.item);
  91.                                 itemQuestion.setBackgroundColor(Color.WHITE);
  92.                         }*/
  93.                         QuestionModel item = items.get(position);
  94.                         ImageView image = (ImageView)convertView.findViewById(R.id.image);
  95.                         image.setImageBitmap(item.getImage());
  96.                         TextView question = (TextView)convertView.findViewById(R.id.question);
  97.                         question.setText(item.getQuestion());
  98.                        
  99.                         final CheckBox chkYes = (CheckBox)convertView.findViewById(R.id.chkYes);
  100.                         final CheckBox chkNo = (CheckBox)convertView.findViewById(R.id.chkNo);
  101.                         chkYes.setChecked(item.getYes());
  102.                         chkNo.setChecked(item.getNo());
  103.                         chkYes.setOnCheckedChangeListener(new OnCheckedChangeListener(){
  104.                                 @Override
  105.                                 public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  106.                                         Toast.makeText(MultipleCheckBox.this, "Yes Button position " + position + " : " + isChecked, Toast.LENGTH_LONG).show();
  107.                                 }
  108.                                
  109.                         });                    
  110.                         chkNo.setOnCheckedChangeListener(new OnCheckedChangeListener(){
  111.                                 @Override
  112.                                 public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  113.                                         Toast.makeText(MultipleCheckBox.this, "No Button position " + position + " : " + isChecked, Toast.LENGTH_LONG).show();
  114.                                 }
  115.                                
  116.                         });
  117.                        
  118.                         return convertView;
  119.                 }
  120.                
  121.         }
  122. }
  123.  
  124.  
Parsed in 0.046 seconds, using GeSHi 1.0.8.4
Attachments
multiplecheckbox.PNG
multiplecheckbox.PNG (40.05 KiB) Viewed 1538 times
Ruel B. Lapid
Senior Android Engineer
User avatar
ruelblapid
Experienced Developer
Experienced Developer
 
Posts: 65
Joined: Mon Mar 14, 2011 2:37 am

Re: Multiple CheckBoxes in a ListView Row

Postby mo5ito » Mon Mar 14, 2011 7:23 pm

@ruelblapid:thanks,it works :wink:
mo5ito
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Mar 09, 2011 9:33 am

Re: Multiple CheckBoxes in a ListView Row

Postby ruelblapid » Tue Mar 15, 2011 12:13 am

You're welcome dude :)

Best,
Ruel B. Lapid
Senior Android Engineer
User avatar
ruelblapid
Experienced Developer
Experienced Developer
 
Posts: 65
Joined: Mon Mar 14, 2011 2:37 am

Re: Multiple CheckBoxes in a ListView Row

Postby mo5ito » Sun Mar 27, 2011 9:40 pm

It works very well on the ListView,but Now I'm trying to do the same thing with an ExpandableListView. but The probleme is that i can't Expand any item of the list . here is my code

Code: Select all
package Android.MyExapandableListView;

import android.app.Activity;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class main extends Activity {
    /** Called when the activity is first created. */
    ExpandableListAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.iterator);
        ExpandableListView lv = (ExpandableListView)this.findViewById(R.id.ExpandableListView);
        MyExpandableListAdapter questions = new MyExpandableListAdapter();
        lv.setAdapter(questions);
       

       
       
    }
   
   
    public class MyExpandableListAdapter extends BaseExpandableListAdapter {
       
       private LayoutInflater inflater =null;
       private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
        private String[][] children = {
                { "Arnold", "Barry", "Chuck", "David" },
                { "Ace", "Bandit", "Cha-Cha", "Deuce" },
                { "Fluffy", "Snuggles" },
                { "Goldy", "Bubbles" }
        };
       
        public MyExpandableListAdapter()

        {

                 this.inflater = main.this.getLayoutInflater();                     

         }
       
        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }
       

        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        public int getGroupCount() {
            return groups.length;
        }

        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
       
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                View convertView, ViewGroup parent) {
           convertView = inflater.inflate(R.layout.child, null);
            TextView question = (TextView)convertView.findViewById(R.id.nomChild);
            question.setText(getChild(groupPosition, childPosition).toString());
            return convertView;
        }
       
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                ViewGroup parent) {
           // TextView textView = getGenericView();
           // textView.setText(getGroup(groupPosition).toString());
            convertView = inflater.inflate(R.layout.group, null);
            TextView question = (TextView)convertView.findViewById(R.id.nomGroup);
            question.setText(getGroup(groupPosition).toString());
            return convertView;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
       
       
       
        public boolean hasStableIds() {
            return true;
        }
       
       
       
       
       
    }
}


Thx for Your help
mo5ito
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Mar 09, 2011 9:33 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 13 guests