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

Why Domain-Specific AI Agents Beat One Big Agent

Everyone is building agents right now. Real estate firms. Independent insurance brokers. Fortune 500 companies with budgets big enough to hire an army of consultants. Ask around and you'll hear the same story everywhere: "we're building our own agent." And yet almost nobody is asking the obvious question: why does the default approach keep failing? One large, general-purpose agent gets wired up to every tool the business owns. It impresses in the demo. Then it quietly stalls before production. There's a gap between what businesses want and what they're actually getting. They want AI woven into their data, their workflows, their day-to-day operations. What they get instead is one oversized agent trying to be a sales rep, a compliance officer, and a customer support line, all at once. That gap is an architecture problem, not a model problem. Key Takeaways The default "one big agent" pattern breaks down on context bloat, cost, fragility, and portability...

ANDROID - update status Linked-IN

jars: 1. linkedin-j-android.jar 2. scribe-1.3.1.jar MainActivity: package com.testshare; import java.util.EnumSet; import org.scribe.oauth.OAuthService; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import com.google.code.linkedinapi.client.LinkedInApiClient; import com.google.code.linkedinapi.client.LinkedInApiClientFactory; import com.google.code.linkedinapi.client.enumeration.NetworkUpdateType; import com.google.code.linkedinapi.schema.Network; public class TestshareActivity extends Activity { private int LINKEDIN_OAUTH_RESULT_CODE = 4000; private OAuthService service; private static final String PROTECTED_RESOURCE_URL = "http://api.linkedin.com/v1/people/~"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(...

Stop Babysitting your AI Coding Agent Every Single Session

  Watched a video by DSquaredLabs on " No more re-explaining: Give your AI agent a memory graph " — here's what stuck with me and why I think this three-tool system is worth your attention If you work on a large codebase with AI coding tools, you already know the ritual. Open a new chat, paste your file structure, explain your stack, describe how the modules connect — and repeat the whole thing tomorrow. It is not a minor inconvenience. It is a compounding productivity tax that gets worse as your project grows. This article breaks down a three-tool system designed to fix exactly that. If you want the full walkthrough with a live demo on a real monorepo, the video at the bottom covers everything in depth — but the core concepts are worth understanding before you dive in. The Problem With How Most Developers Use AI Today Most AI-assisted coding workflows are missing three things that every good engineering team actually has: A map of the codebase A process for how w...