Checkbox Text List :: Extension of Iconified Text tutorial

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Re: Checkbox Text List :: Extension of Iconified Text tutor

Postby strider2023 » Fri Nov 12, 2010 1:44 pm

@limteckchai

Was just going through the post....dunno if u got ur ans but u can try this site

http://www.androidpeople.com/android-custom-listview-tutorial-example/

Hope this helps...to anybody who is looking for a customized List View. If you have queries do let me know as I have already worked on this. :D
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Top

Re: Checkbox Text List :: Extension of Iconified Text tutor

Postby nsgocev » Mon Nov 22, 2010 8:04 am

Hello. I have a different problem. When I use the selectAll() option and attach it to a checkbox notifyDataSetInvalidated(); reverses the order of the items in the list container. Any ideas how can I solve this ?
nsgocev
Freshman
Freshman
 
Posts: 3
Joined: Mon Nov 22, 2010 8:01 am

Re: Checkbox Text List :: Extension of Iconified Text tutor

Postby heepie » Tue Aug 02, 2011 4:30 pm

I had the same problem with the checkboxes getting checked and unchecked at random. The problem was with the getView() method.

I include the code below. Hope this helps somebody !!!
ListAcitivy
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DriveStarActivity extends ListActivity implements OnClickListener {
  2.        
  3.         private ListItemAdapter listItemAdapter;
  4.         private Button btnAdd = null;
  5.         private EditText etxtNew = null;
  6.        
  7.     @Override
  8.     public void onCreate(Bundle icicle) {
  9.         super.onCreate(icicle);
  10.         setContentView(R.layout.list_activity);
  11.        
  12.         /*
  13.          * Set and fill the Adapter
  14.          */
  15.         listItemAdapter = new ListItemAdapter(this);
  16.         setListAdapter(listItemAdapter);
  17.        
  18.         Drawable drawable = getResources().getDrawable(R.drawable.icon);
  19.        
  20.         for(int k = 0; k < 100; k++)
  21.         {
  22.                 listItemAdapter.addItem(new ListItem(false, "Item " + Integer.toString(k), drawable));
  23.         }
  24.        
  25.         /*
  26.          * Add Button
  27.          */
  28.         btnAdd = (Button) findViewById(R.id.btnAdd);
  29.         btnAdd.setOnClickListener(this);
  30.        
  31.         etxtNew = (EditText) findViewById(R.id.etxtNew);
  32.     }
  33.    
  34.     /*
  35.      * Add a new Item to the list
  36.      */
  37.         @Override
  38.         public void onClick(View v) {
  39.                 Drawable drawable = getResources().getDrawable(R.drawable.icon);
  40.                
  41.                 listItemAdapter.addItem(new ListItem(false, etxtNew.getText().toString(), drawable));
  42.         }
  43. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Adapter
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ListItemAdapter extends BaseAdapter implements OnCheckedChangeListener, OnClickListener {
  2.         private static final String TAG = ListItemAdapter.class.getSimpleName();
  3.  
  4.         private Context context;
  5.         private LayoutInflater mInflater = null;
  6.         private List<ListItem> mItems = new ArrayList<ListItem>();      // Our List of Items from ListItem.java
  7.  
  8.         public ListItemAdapter(Context context) {
  9.                 Log.d(TAG, "Constructor");
  10.                 this.context = context;
  11.                 mInflater = LayoutInflater.from(context);
  12.         }
  13.  
  14.         public void addItem(ListItem it) {
  15.                 mItems.add(it);                                                                                 // Adding Items to the list
  16.         }
  17.  
  18.         public void setListItems(List<ListItem> lit) {                          // Adding Items by passing a whole new List
  19.                 mItems = lit;
  20.         }
  21.  
  22.         public int getCount() {
  23.                 return mItems.size();
  24.         }
  25.  
  26.         public Object getItem(int position) {
  27.                 return mItems.get(position);
  28.         }
  29.  
  30.         public long getItemId(int position) {
  31.                 return position;
  32.         }
  33.        
  34.         /*
  35.          * This gets called every time ListView needs a new Row Item
  36.          * position holds the position on the row in the ListView
  37.          * convertView is the new view we have to filled with our custom --> list_item.xml
  38.          */
  39.         public View getView(int position, View convertView, ViewGroup parent) {
  40.                 ViewHolder vHolder = null;
  41.  
  42.                 if (convertView != null)
  43.                         vHolder = (ViewHolder) convertView.getTag();                                                            // convertView is been recycled
  44.                 else {
  45.                         convertView = (View) mInflater.inflate(R.layout.list_item, null);                       // Set content of new View with list_item.xml
  46.  
  47.                         vHolder = new ViewHolder();
  48.                         vHolder.checkBox = ((CheckBox) convertView.findViewById(R.id.lstChkbox));       // Getting pointers
  49.                         vHolder.textView = ((TextView) convertView.findViewById(R.id.lstText));
  50.                         vHolder.imageView = ((ImageView) convertView.findViewById(R.id.listImage));
  51.  
  52.                         vHolder.checkBox.setOnCheckedChangeListener(this);                                                      // Setting Listeners
  53.                         vHolder.imageView.setOnClickListener(this);
  54.                        
  55.                         convertView.setTag(vHolder);
  56.                 }
  57.  
  58.                 vHolder.checkBox.setId(position);                                                                                               // This is part of the Adapter APi
  59.                 vHolder.textView.setId(position);                                                                                               // Do not delete !!!
  60.                 vHolder.imageView.setId(position);
  61.  
  62.                
  63.                 if (mItems.get(position).getChecked()) {                                                                                // Setting parameters for the View from our mItems list
  64.                         vHolder.checkBox.setChecked(true);
  65.                 } else {
  66.                         vHolder.checkBox.setChecked(false);
  67.                 }
  68.  
  69.                 vHolder.textView.setText(mItems.get(position).getText());
  70.                 vHolder.imageView.setImageDrawable(mItems.get(position).getmImage());
  71.                
  72.                 return convertView;
  73.         }
  74.        
  75.         public static class ViewHolder {
  76.                 CheckBox checkBox;
  77.                 TextView textView;
  78.                 ImageView imageView;
  79.         }
  80.  
  81.         /*
  82.          * Ok for this test but Toast are going to show every time the row comes into View
  83.          */
  84.         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  85.                 Log.d(TAG, "Checked");
  86.                 int position = buttonView.getId();
  87.  
  88.                 if (isChecked) {
  89.                         mItems.get(position).setChecked(true);
  90.                         Toast.makeText(context, mItems.get(position).getText(), Toast.LENGTH_LONG).show();
  91.                 } else {
  92.                         mItems.get(buttonView.getId()).setChecked(false);
  93.                 }
  94.         }
  95.  
  96.         @Override
  97.         public void onClick(View v) {
  98.                 Toast.makeText(context, "ImageClicked", Toast.LENGTH_LONG).show();
  99.         }
  100.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


Row item
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /**
  2.  * Each Row Item in our List is store here and used in our adapter
  3.  */
  4. public class ListItem implements Comparable<ListItem> {
  5.  
  6.         private String mText = "";
  7.         private boolean mChecked;
  8.         private Drawable mImage;
  9.  
  10.         public ListItem(boolean checked, String text, Drawable imageView) {
  11.                 setChecked(checked);
  12.                 setText(text);
  13.                 setmImage(imageView);
  14.         }
  15.  
  16.         public void setChecked(boolean value) {
  17.                 this.mChecked = value;
  18.         }
  19.  
  20.         public boolean getChecked() {
  21.                 return this.mChecked;
  22.         }
  23.  
  24.         public String getText() {
  25.                 return mText;
  26.         }
  27.  
  28.         public void setText(String text) {
  29.                 mText = text;
  30.         }
  31.  
  32.         public Drawable getmImage() {
  33.                 return mImage;
  34.         }
  35.  
  36.         public void setmImage(Drawable mImage) {
  37.                 this.mImage = mImage;
  38.         }
  39.  
  40.         /** Make ListItem comparable by its name */
  41.         public int compareTo(ListItem other) {
  42.                 if (this.mText != null)
  43.                         return this.mText.compareTo(other.getText());
  44.                 else
  45.                         throw new IllegalArgumentException();
  46.         }
  47. }
  48.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Main Layout
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.    android:orientation="vertical"
  5.    android:layout_width="fill_parent"
  6.    android:layout_height="fill_parent" >
  7.    
  8.     <TextView
  9.         android:layout_width="fill_parent"
  10.        android:layout_height="wrap_content"
  11.        android:layout_gravity="center"
  12.         android:text="List of Items"
  13.         android:textSize="30sp" />
  14.    
  15.     <LinearLayout
  16.         android:id="@+id/etxtsLayout"
  17.         android:orientation="vertical"
  18.         android:layout_gravity="center_vertical"
  19.         android:layout_height="match_parent"
  20.         android:layout_width="fill_parent"
  21.         android:layout_weight="1" >
  22.            
  23.             <ListView
  24.        android:id="@android:id/list"
  25.        android:layout_width="fill_parent"
  26.        android:layout_height="fill_parent"
  27.        android:drawSelectorOnTop="false"
  28.        android:choiceMode="multipleChoice"
  29.        />
  30.                
  31.            
  32.         </LinearLayout>
  33.    
  34.     <LinearLayout
  35.         android:layout_width="match_parent"
  36.         android:layout_height="wrap_content" >
  37.        
  38.         <EditText
  39.                 android:hint="Add new item"
  40.                 android:id="@+id/etxtNew"
  41.                 android:layout_height="wrap_content"
  42.                 android:layout_width="wrap_content"
  43.                 android:layout_weight="1" />
  44.        
  45.         <Button
  46.                 android:text="Add"
  47.                 android:id="@+id/btnAdd"
  48.                 android:layout_height="wrap_content"
  49.                 android:layout_width="wrap_content"
  50.                  />
  51.    
  52.     </LinearLayout>
  53.  
  54. </LinearLayout>
  55.  
Parsed in 0.005 seconds, using GeSHi 1.0.8.4


row item
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="wrap_content"
  6.         android:orientation="horizontal" >
  7.        
  8.         <CheckBox
  9.                 android:id="@+id/lstChkbox"
  10.                 android:layout_width="wrap_content"
  11.                 android:layout_height="wrap_content" />
  12.        
  13.         <TextView
  14.                 android:layout_width="wrap_content"
  15.                 android:layout_height="wrap_content"
  16.                 android:id="@+id/lstText"
  17.                 android:textSize="30px"
  18.                 android:layout_weight="1" />
  19.                
  20.         <ImageView
  21.                 android:id="@+id/listImage"
  22.                 android:layout_height="wrap_content"
  23.                 android:layout_width="wrap_content" />
  24.                
  25. </LinearLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
heepie
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Aug 02, 2011 4:10 pm

Re: Checkbox Text List :: Extension of Iconified Text tutor

Postby linhlt » Tue Feb 14, 2012 6:19 am

Hello. How can i change the color when i click the row. I'm new to Android and i don't know how to do this. Thank you so much!
linhlt
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Feb 14, 2012 6:14 am

Top
Previous

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests