Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7595053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:34:31+00:00 2026-05-30T21:34:31+00:00

I have defined onCheckedChanged for the checkbox in my listview. When i click on

  • 0

I have defined onCheckedChanged for the checkbox in my listview.
When i click on the check box to check / uncheck it this function is getting invoked.
But when i setthe state of the check box from code like

check.setChecked(true);

the onCheckedChanged is not getting invoked.

Please help.

Adapter file :

 package com.idg.project.adapters;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.idg.project.R;
import com.idg.project.activities.ScanListActivity;
import com.idg.project.activities.SearchResultActivity;
import com.idg.project.adapters.WishListAdapter.ViewHolder;
import com.idg.project.entity.ScannedProduct;

public class ScanListAdapter extends BaseAdapter {
    private Context context;
    private List<ScannedProduct> productList;
    protected LayoutInflater mInflater;
    Button showOrHideButton;
    static public int count = 0;
    String barcodeForSelectedRow;
    String formatForSelectedRow;
    OnItemClickListener rowListener;
    Activity parentActivity;
    boolean isWishList;

    public ScanListAdapter(Context context, List<ScannedProduct> objects,
            Button button, Activity parentActivity) {

        super();
        this.productList = objects;
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        showOrHideButton = button;
        this.parentActivity = parentActivity;
        this.isWishList = isWishList;
    }

    public int getCount() {

        return productList.size();
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }
@Override
public void notifyDataSetChanged() {
    // TODO Auto-generated method stub
    super.notifyDataSetChanged();
}
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final int pos = position;
        Log.i("checkboxflag at : ", pos+"is"+(productList.get(pos).getCheckboxflag()));


            Log.i("getview : fresh", "getview"+pos);
            convertView = mInflater.inflate(R.layout.product_list_row, null);

            holder = new ViewHolder();
            holder.text1 = (TextView) convertView.findViewById(R.id.productid);
            holder.text1.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Intent intent = new Intent(parentActivity,
                            SearchResultActivity.class);
                    intent.putExtra("barcode", productList.get(pos)
                            .getBarcode());
                    intent.putExtra("format", productList.get(pos).getFormat());
                    intent.putExtra("IsScan", false);
                    Log.i("", "" + productList.get(pos).getBarcode());
                    parentActivity.startActivity(intent);
                    Log.i("", "" + pos);

                }
            });
            holder.text2 = (TextView) convertView.findViewById(R.id.price);
            // holder.text2.setOnClickListener(listener);
            holder.image = (ImageView) convertView
                    .findViewById(R.id.productimageid);
            convertView.setTag(holder);
            // holder.image.setOnClickListener(listener);




        holder.text1.setText(productList.get(position).getTitle());
        holder.text2.setText(productList.get(position).getPrice().toString());
        if (productList.get(position).getSmallImage() != null) {
            byte[] bb = (productList.get(position).getSmallImage());
            holder.image.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0,
                    bb.length));
        } else {
            holder.image.setImageBitmap(null);
            holder.image.setBackgroundResource(R.drawable.highlight_disabled);
        }

        // holder.image.setImageBitmap(Utils.loadBitmap(productList.get(position).getSmallImage()));
        final CheckBox check = (CheckBox) convertView
                .findViewById(R.id.checkbox);
        check.setClickable(true); // to remove anything carried over from prev convert view
        if(productList.get(pos).getCheckboxflag()==1)
        {
            Log.i("CheckBox set checked",""+pos);
            check.setChecked(true);

        }
        else{
            Log.i("CheckBox set unchecked",""+pos);
            check.setChecked(false);
        }
        setWishListItemsInScanList(pos, convertView);
        check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                Log.i("OnclickListener","Current Position"+pos);
                if (check.isChecked()
                        && productList.get(pos).getWishListFlag() == 0) {

                    if(check.isClickable()){
                        Log.i("CheckBox check",""+pos);
                    ScanListActivity.updateCheckBoxSelection(1, pos);
                    ScanListAdapter.count++;
                    }

                } else if (!check.isChecked()
                        && productList.get(pos).getWishListFlag() == 0){
                    if(check.isClickable()){
                        ScanListActivity.updateCheckBoxSelection(0, pos);
                        ScanListAdapter.count--;
                        Log.i("CheckBox UNcheck",""+pos);
                        }



                }
                if (ScanListAdapter.count == 0) {
                    // showOrHideButton.setClickable(false);
                    // showOrHideButton.setVisibility(View.GONE);
                    showOrHideButton.setEnabled(false);
                } else {
                    // showOrHideButton.setVisibility(View.VISIBLE);
                    showOrHideButton.setEnabled(true);
                }

            }

        });
        return convertView;
    }

    private void setWishListItemsInScanList(int pos, View convertView) {
        if (productList.get(pos).getWishListFlag() == 1) {
            Log.i("CheckBox set checked from wish list",""+pos);
            CheckBox check = (CheckBox) convertView.findViewById(R.id.checkbox);
            check.setClickable(false);
            check.setChecked(true); 
        }

    }

    static class ViewHolder {
        TextView text1;
        ImageView image;
        TextView text2;
    }

}

List activity file :

   package com.idg.project.activities;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.idg.project.R;
import com.idg.project.adapters.WishListAdapter;
import com.idg.project.adapters.ScanListAdapter;
import com.idg.project.entity.ScannedProduct;
import com.idg.project.services.ScannedProductDataAccessManager;

public class ScanListActivity extends BaseActivity {
    static Button scanlist;
    ScanListAdapter listAdapter;
    static List<ScannedProduct> productList;

    /* Notes for the Developer : 
     * For tracking the checked items  Checkboxflag 
     * is maintained. 
     * Point1 : Select all will just set this flag in the local list and then call notifyDatachange of the adapter
     * within adapter the check box is set or reset based on this flag for each row
     * 
     * Point 2: When individual rows are selected , there is an onclick of the check box is invoked
     * Here the Checkboxflag of the local list is set /unset . Also we need a way to knpw the select all button is 
     * to enabled or diabled. for that Count variable is updated here. 
     * Now Important point is these two actions shoulnt be taking place if the checkbox state change due to select all
     * So there is a special check of isclickable in the onclicklistener 
     * 
     * Point 3: In scan list the items in the wish list are to be marked. This again needs special logic. 
     * This is done in the adapter code by checking all the rows whose wishListFlag is 1 and making it non clickable
     * 
     * Important : Listview has the concept of ViewGroup and each view group is usually the rows fitting in the display screen
     * so when we scroll, the viewGropu changes. 
     * Convertview is get reused for view groups. So need to careful undesired values that will be carried to next viewgroup*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.scan_list);
        productList = new ArrayList<ScannedProduct>();
        productList = getProductList();
        for(int i=0;i<productList.size();i++){
            Log.i("checkboxflag at : ", i+"is"+(productList.get(i).getCheckboxflag()));
        }
        final ListView lv = (ListView) findViewById(R.id.list);
        scanlist = (Button) findViewById(R.id.addtowishlist);
        scanlist.setEnabled(false);
        listAdapter = new ScanListAdapter(this, productList, scanlist, this);
        lv.setAdapter(listAdapter);
    }

    private List<ScannedProduct> getProductList() {
        List<ScannedProduct> productList = new ArrayList<ScannedProduct>();
        ScannedProductDataAccessManager productDataBaseManager = new ScannedProductDataAccessManager(
                getApplicationContext());
        String[] colList = { "title", "smallImage", "price" };
        productList = productDataBaseManager.fetchAllProducts();
        return productList;
    }

    static boolean selectFlag = false;

    public void selectAll(View view) {
        ListView listView = (ListView) findViewById(R.id.list);
        view = findViewById(R.id.select_all);
        if (selectFlag == false) {
            for (int i = 0; i < listView.getAdapter().getCount(); i++) {
                productList.get(i).setCheckboxflag(1);
            }
            view.setBackgroundResource(R.drawable.login_remme_dwn_btn);
            selectFlag = true;
            TextView text=(TextView) findViewById(R.id.select);
            text.setText("Unselect All");
            scanlist.setEnabled(true);
        } else {
            for (int i = 0; i < listView.getAdapter().getCount(); i++) {
                productList.get(i).setCheckboxflag(0);
            }
            view.setBackgroundResource(R.drawable.login_remme_up_btn);
            selectFlag = false;
            TextView text=(TextView) findViewById(R.id.select);
            text.setText("Select All");
            scanlist.setEnabled(false);

        }
        ((BaseAdapter)listView.getAdapter()).notifyDataSetChanged(); // we are only setting the flags in the list
                                            // so need to notify adapter to reflect same on checkbox state
        //listView.refreshDrawableState();
    }

    public void addToWishList(View view) {
        ListView listView = (ListView) findViewById(R.id.list);
        for (int i = 0; i < listView.getAdapter().getCount(); i++) {

                ScannedProduct product = productList.get(i);
                if (product.getWishListFlag() == 0 && product.getCheckboxflag()==1) {
                    product.setWishListFlag(1);
                    new ScannedProductDataAccessManager(getApplicationContext())
                            .updateProduct(product, "title",
                                    new String[] { product.getTitle() });
                    product.setCheckboxflag(0);
                    //ScanListAdapter.count--;
                }
                Log.i("ScanList selected", product.getTitle());

            }


        Toast.makeText(getApplicationContext(),
                "Added selected items to Wish List", Toast.LENGTH_SHORT).show();
        scanlist.setEnabled(false);
        ((BaseAdapter)listView.getAdapter()).notifyDataSetChanged();

    }
    static public void updateCheckBoxSelection(int flag,int pos){   // when individual row check box is checked/ unchecked
                                                                // this fn is called from adapter to update the list
        productList.get(pos).setCheckboxflag(flag);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:34:32+00:00Added an answer on May 30, 2026 at 9:34 pm

    I got the answer / bug in my code

    1. i am not reusing convertview so its every time a new holder.

    2. I am changing the flag of the checkbox and then assigning a statechange listener for the checkbox
      thus its not getting invoked

    when i changed the order to assign checkchangelistener before actually changing the state , its working as expected. The listener is getting called.

    thanks all of you

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined this Entity class, but it does not seem to save the
I have defined a tag with a CSS attribute overflow set to scroll. This
I have defined a boolean Jess function that accepts a lambda, as follows: (deffunction
I have defined an AIDL file with my interface. Something like this: interface IResPlugin
I have defined new member in my class protected COMObject.Call call_ = null; This
/*I have defined Extension Methods for the TypeX like this*/ public static Int32 GetValueAsInt(this
I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
I have defined a function called login that should return a token. The token
I have defined a BitmapButtonField in Blackberry. But the default grey color background does
I have defined c as char c[][10] in function definition and used it like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.