Multi-Selection ListView ANDROID with CheckBox PART-2

12:45:00 Dhaval Sodha Parmar 9 Comments



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...

9 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
    Replies
    1. i have added code in below comment.. please check it....

      Delete
  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
  6. thanks 4 the code..

    Now I want to make the selected item move to another page (example: from MainPage to FavoritePage). Then when we uncheck, delete it from list (in FavoritePage).

    Can you show me the code if you have it.

    tq so much.

    ReplyDelete