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

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

Connecting mysql Database in ANDROID using PHP & JSON

 To implement this tutorial you should have basic knowledge of how to run PHP script and start server.  If we talk about client-server architecture, client is Android device and in server side there is a combination of PHP Script and MySQL. In short, PHP Script sits in middle as shown in image. Lets suppose that we have a MySQL database named Employee, and a table int created, with the following SQL: CREATE TABLE `employee` (   `emp_id` int(11) NOT NULL auto_increment,   `emp_name` varchar(100) NOT NULL,   PRIMARY KEY  (`emp_id`) ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; The PHP code will be very simple: Now Create Android Project :  The Android part is only a bit more complicated: -use a HttpPost to get the data -convert response to string -parse JSON data in to List In Your First Activity : O/P :