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

Post a Comment

Popular posts from this blog

ANDROID - Adding ActionBar Navigation Tabs

Note: if you are develop App < 3.0 Android OS then use  ActionBarSherlock   support library. ActionBarSherlock is an extension of the  support library  designed to facilitate the use of the action bar design pattern across all versions of Android with a single API. Create new Android Project : in Main Activity package com.AlarmManager; import android.os.Bundle; import android.view.View; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar.Tab; import com.actionbarsherlock.app.SherlockFragmentActivity; public class AlarmManagerActivity extends SherlockFragmentActivity { public static String ACTIVE_TAB = "activeTab"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { setTheme(R.style.Theme_Sherlock_Light_DarkActionBar); super.onCreate(savedInstanceState); // setContentView(R.lay

Android Material Design (with Design support Library) - 4

Introducing  Design Support Library  , to use Material design component in older version API - android 2.1 and above.  You’ll find a navigation drawer view, floating labels for editing text, a floating action button, snackbar, tabs, and a motion and scroll framework to tie them together. I have used my previous example, so its easy for demonstrate.  Note: Update your Android SDK support  repositories, support library if not updated i  used compileSdkVersion 'android-MNC' for Android M but you can change it to build in older API add  dependencies in build.gradle file compile 'com.android.support:appcompat-v7:22.2.0' compile 'com.android.support:design:22.2.0' compile 'com.android.support:support-v4:22.2.0' start with  navigation drawer , its very easy to use lets, design for drawer <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://

Android show Data from Sqlite DB into Grid View

Shaadi.com Matrimonials Shaadi.com Indian Matrimonials Your Main Activity class package com . Sqlite_grid_view ; import java . util . ArrayList ; import java . util . List ; import android . app . Activity ; import android . os . Bundle ; import android . util . Log ; import android . view . View ; import android . widget . AdapterView ; import android . widget . AdapterView . OnItemClickListener ; import android . widget . ArrayAdapter ; import android . widget . GridView ; import android . widget . TextView ; import android . widget . Toast ; public class AndroidSQLiteTutorialActivity extends Activity { private GridView gridView ; public static ArrayList < String > ArrayofName = new ArrayList < String >(); /** Called when the activity is first created. */ @ Override public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . l