Skip to main content

Multi-Selection ListView ANDROID with CheckBox PART-2



Check Below Code : mainActivity.java

 package com.example.listviewcheckbox;  
 import java.util.ArrayList;  
 import android.app.Activity;  
 import android.content.Context;  
 import android.graphics.Bitmap;  
 import android.os.Bundle;  
 import android.util.Log;  
 import android.view.LayoutInflater;  
 import android.view.Menu;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.view.ViewGroup;  
 import android.widget.BaseAdapter;  
 import android.widget.CheckBox;  
 import android.widget.ImageView;  
 import android.widget.ListView;  
 import android.widget.TextView;  
 import android.widget.Toast;  
 public class MainActivity extends Activity {  
      private ListView listview;  
      ArrayList<String> items = new ArrayList<String>();  
      private int count;  
      private boolean[] thumbnailsselection;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           fillarray();  
           count = items.size();  
           thumbnailsselection = new boolean[count];  
           listview = (ListView) findViewById(R.id.listView1);  
           listview.setAdapter(new ImageAdapter(MainActivity.this));  
      }  
      private void fillarray() {  
           // TODO Auto-generated method stub  
           items.clear();  
           items.add("Android alpha");  
           items.add("Android beta");  
           items.add("1.5 Cupcake (API level 3)");  
           items.add("1.6 Donut (API level 4)");  
           items.add("2.0 Eclair (API level 5)");  
           items.add("2.0.1 Eclair (API level 6)");  
           items.add("2.1 Eclair (API level 7)");  
           items.add("2.2–2.2.3 Froyo (API level 8)");  
           items.add("2.3–2.3.2 Gingerbread (API level 9)");  
           items.add("2.3.3–2.3.7 Gingerbread (API level 10)");  
           items.add("3.0 Honeycomb (API level 11)");  
           items.add("3.1 Honeycomb (API level 12)");  
           items.add("3.2 Honeycomb (API level 13)");  
           items.add("4.0–4.0.2 Ice Cream Sandwich (API level 14)");  
           items.add("4.0.3–4.0.4 Ice Cream Sandwich (API level 15)");  
           items.add("4.1 Jelly Bean (API level 16)");  
           items.add("4.2 Jelly Bean (API level 17)");  
           items.add("5.0 Key Lime Pie (API level 18)");  
      }  
      @Override  
      public boolean onCreateOptionsMenu(Menu menu) {  
           // Inflate the menu; this adds items to the action bar if it is present.  
           getMenuInflater().inflate(R.menu.activity_main, menu);  
           return true;  
      }  
      public class ImageAdapter extends BaseAdapter {  
           private LayoutInflater mInflater;  
           private Context mContext;  
           public ImageAdapter(Context context) {  
                mContext = context;  
           }  
           public int getCount() {  
                return count;  
           }  
           public Object getItem(int position) {  
                return position;  
           }  
           public long getItemId(int position) {  
                return position;  
           }  
           public View getView(int position, View convertView, ViewGroup parent) {  
                ViewHolder holder;  
                if (convertView == null) {  
                     holder = new ViewHolder();  
                     convertView = LayoutInflater.from(mContext).inflate(  
                               R.layout.row_photo, null);  
                     holder.textview = (TextView) convertView  
                               .findViewById(R.id.thumbImage);  
                     holder.checkbox = (CheckBox) convertView  
                               .findViewById(R.id.itemCheckBox);  
                     convertView.setTag(holder);  
                } else {  
                     holder = (ViewHolder) convertView.getTag();  
                }  
                holder.checkbox.setId(position);  
                holder.textview.setId(position);  
                holder.checkbox.setOnClickListener(new OnClickListener() {  
                     public void onClick(View v) {  
                          // TODO Auto-generated method stub  
                          CheckBox cb = (CheckBox) v;  
                          int id = cb.getId();  
                          if (thumbnailsselection[id]) {  
                               cb.setChecked(false);  
                               thumbnailsselection[id] = false;  
                          } else {  
                               cb.setChecked(true);  
                               thumbnailsselection[id] = true;  
                          }  
                     }  
                });  
                holder.textview.setOnClickListener(new OnClickListener() {  
                     public void onClick(View v) {  
                          // TODO Auto-generated method stub  
                          int id = v.getId();  
                     }  
                });  
                holder.textview.setText(items.get(position));  
                holder.checkbox.setChecked(thumbnailsselection[position]);  
                holder.id = position;  
                return convertView;  
           }  
      }  
      class ViewHolder {  
           TextView textview;  
           CheckBox checkbox;  
           int id;  
      }  
      public void click(View v) {  
           if (v.getId() == R.id.button1) {  
                final ArrayList<Integer> posSel = new ArrayList<Integer>();  
                posSel.clear();  
                boolean noSelect = false;  
                for (int i = 0; i < thumbnailsselection.length; i++) {  
                     if (thumbnailsselection[i] == true) {  
                          noSelect = true;  
                          Log.e("sel pos thu-->", "" + i);  
                          posSel.add(i);  
                          // break;  
                     }  
                }  
                if (!noSelect) {  
                     Toast.makeText(MainActivity.this, "Please Select Item!",  
                               Toast.LENGTH_SHORT).show();  
                } else {  
                     Toast.makeText(MainActivity.this,  
                               "Selected Items:" + posSel.toString(),  
                               Toast.LENGTH_LONG).show();  
                }  
           }  
      }  
 }  

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
   android:orientation="vertical" >  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:gravity="center"  
     android:orientation="vertical" >  
     <Button  
       android:id="@+id/button1"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:onClick="click"  
       android:text="getCount" />  
   </LinearLayout>  
   <ListView  
     android:id="@+id/listView1"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content" >  
   </ListView>  
 </LinearLayout>  

row_photo.xml

 <?xml version="1.0" encoding="UTF-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent" >  
   <TextView  
     android:id="@+id/thumbImage"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentLeft="true"  
     android:text="sample text"  
     android:textAppearance="?android:attr/textAppearanceMedium" />  
   <CheckBox  
     android:id="@+id/itemCheckBox"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentRight="true" />  
 </RelativeLayout>  

Enjoy Coding...

Comments

  1. its awesome man !!!! Thanks a lot :)

    ReplyDelete
  2. Hi Dhaval! Can you please demonstrate how can we get a list of all checked items? I would very much appreciate it. :)
    Thank you!

    ReplyDelete
  3. thnks for nic post,if i clcik getcount button i need to remove selected items and remain items i need display in lisview,what changes do i need to do in above posted code

    ReplyDelete
    Replies
    1. Here you get list of selected item position:

      public void click(View v) {
      if (v.getId() == R.id.button1) {
      final ArrayList posSel = new ArrayList();
      posSel.clear();
      boolean noSelect = false;
      for (int i = 0; i < thumbnailsselection.length; i++) {
      if (thumbnailsselection[i] == true) {
      noSelect = true;
      Log.e("sel pos thu-->", "" + i);
      posSel.add(i);
      // break;
      }
      }
      if (!noSelect) {
      Toast.makeText(MainActivity.this, "Please Select Item!",
      Toast.LENGTH_SHORT).show();
      } else {
      Toast.makeText(MainActivity.this,
      "Selected Items:" + posSel.toString(),
      Toast.LENGTH_LONG).show();
      }
      }
      }

      using that position remove items from your arraylist and readapte your adapter with listview or you can use adapter.notifydatastatechange();

      Delete
  4. how about a .zip file , thats the least you can do if your not going to make ur code cut and paste-able

    ReplyDelete
  5. yeah man, if your going to post tutorials you gotta post a .zip to your source or at least find a snippet that will allow us to copy the code for each code chunk to a clipboard and PLEASE for gods sake take off that horriable tag that gets added to your code when we try to cut/paste from your site. it just makes ur site come off as spammy and unprofessional. if u post good, easy to implement code ppl will come back on their own .

    ReplyDelete
    Replies
    1. hey... now you can copy code.... from this blog...

      sorry for inconvenience...

      Delete

Post a Comment

Popular posts from this blog

Android - Google MAP V2 PART 3 (add Polyline)

Download Full Code  of  PART-1 , 2 , 3 , 4 Before go ahead Please look in to PART-1 & PART-2 I have just modify Main Activity class: package com.djandroid.mapsv2; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.OnMapClickListener; import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.Polyline; import com.google.android.gms.maps.model.PolylineOptions; public class MainActivity extends FragmentActivity { private GoogleMap MAP; private boolean markClic...

Android Material Design - 2 (Good bay ActionBar, Hello ToolBar with Navigation Drawer)

hello all, after long time i have update my blog with android material Design theme, i am tying to demonstrate new things which is available with android lollipop. using different example i 'll show you, one sample app with material design which include ToolBar, Floting Actions, Tabs, Cardview, Recycle view. Download full example on  Github . In this example we will take a look at the new Actionbar replacement called Toolbar. It is introduced in Android Lollipop, API level 21 release, and hence it is available to use, out of box for the application that are targeted to 21 and above. However, as always Google provides fully supported Toolbar features to lower android os devices via AppCompact support library. In AppCompat, Toolbar is implemented in the  android.support.v7.widget.Toolbar  class. Pre-requirements: 1. Android Studio 1.0.1 (Latest while writing the post) 2. Appcombat v7 Support librar...

ANDROID - AutoCompleteTextView inside ActionBar

in MainActivity: package com . example . testapp ; import android . annotation . TargetApi ; import android . app . ActionBar ; import android . app . Activity ; import android . content . Context ; import android . os . Bundle ; import android . view . LayoutInflater ; import android . view . View ; import android . widget . ArrayAdapter ; import android . widget . AutoCompleteTextView ; @TargetApi ( 11 ) public class MainActivity extends Activity { private static final String [] COUNTRIES = new String [] { "Belgium" , "France" , "France_" , "Italy" , "Germany" , "Spain" }; @Override public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . layout . activity_main ); ActionBar actionBar = getActionBar (); actionBar . setDisplayHomeAsUpEnabled ( tru...