1

I have stored some value in arraylist if checkbox is clicked but i am having a difficult time to get it from listView class. How can i get it. Also if I keep clickListener in view that contains checkbox and textView, the checkbox click is not working but if i click in textview it works. How to fix it. Below is my code. Thanks in advance

singlerow_compare.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/singleRow" android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textViewCompare" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageViewCompare"/>

</RelativeLayout>

listview xml:

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listViewCompare" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Compare"
    android:id="@+id/compare"
    android:layout_alignParentBottom="true"/>

ListView.java

listView = (HorizontalListView) findViewById(R.id.listViewCompare);
compareBtn = (Button) findViewById(R.id.compare);
listView.setAdapter(new CustomAdapter(this, nameList, imageList)); 
compareBtn.setOnClickListener(new View.OnClickListener()
    {   @Override
            public void onClick(View view) {
            //I need to have addCheckBoxValue arraylist from adapter here
            }
    }

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {
ArrayList result;
Context context;
Drawable [] imageId;
protected ArrayList<String> addCheckBoxValue;

private static LayoutInflater inflater=null;
public CustomAdapter(CompareListView mainActivity, ArrayList nameList, Drawable[] imageList) {
    result=nameList;
    context=mainActivity;
    imageId=imageList;
    inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public class Holder
{
    TextView tv;
    ImageView img;
    CheckBox checkBox;
}

@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
    addCheckBoxValue = new ArrayList();
    final Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.singlerow_compare, null);
    holder.tv=(TextView) rowView.findViewById(R.id.textViewCompare);
    holder.checkBox = (CheckBox) rowView.findViewById(R.id.imageViewCompare);
    holder.tv.setText(result.get(i).toString());
    holder.checkBox.setButtonDrawable(imageId[i]);
    final String selectedCbValue = holder.tv.getText().toString();
    holder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            boolean checked = ((CheckBox) view).isChecked();
            if(checked){
                if (!addCheckBoxValue.contains(selectedCbValue))
                    addCheckBoxValue.add(selectedCbValue);
            }else{
                if (addCheckBoxValue.contains(selectedCbValue))
                    addCheckBoxValue.remove(selectedCbValue);
            }
        }
    });
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
     //if i click on textView, it works but when i click on checkbox here, it doesnt work why?? So i have clicklistener in checkbox abov
        }
    });
    return rowView;
}
2
  • Use an interface to get the values ,define the interface in your adapter and implement it in your activity Commented Jun 13, 2016 at 6:57
  • you need to maintain seperate array for checkbox to store values Commented Jun 13, 2016 at 6:59

2 Answers 2

1

Create Following method in your CustomAdapter class:

public  ArrayList<String> getCheckBoxValue(){
    return addCheckBoxValue;
}

And in your ListActivity. Change your activity code like this:

private CustomAdapter listAdapter; // declare this before on create

Now where you are setting adapter in your on create write this code:

listAdapter = new CustomAdapter(this, nameList, imageList)
// set Adapter like this:
listView.setAdapter(listAdapter);

your button on click code should be like this:

compareBtn.setOnClickListener(new View.OnClickListener()
    {   @Override
            public void onClick(View view) {
            //I need to have addCheckBoxValue arraylist from adapter here
             listAdapter.getCheckBoxValue(); // do whatever you want to do here

            }
    }

Happy Coding!!!

Sign up to request clarification or add additional context in comments.

1 Comment

tysm. I tried intializing adapter class but did it after listView.setAdapter() & thats error. I didnt saw that and lost hours. thankyou.....
1

I suggest you make boolean array for checkbox and maintain it like this way,this worked for me

public class CustomAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private final Context context;
    private List<ModelPooja> listData;

    public CustomAdapter(Context mainActivity, List<ModelPooja> listData) {
        context = mainActivity;
        this.listData = listData;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
            holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
            holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.checks.setOnCheckedChangeListener(null);
        holder.checks.setFocusable(false);

        if (listData.get(position).isselected) {
            holder.checks.setChecked(true);
        } else {
            holder.checks.setChecked(false);
        }

        holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton cb, boolean b) {

                if (checkMaxLimit()) {

                    if (listData.get(position).isselected && b) {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;

                    } else {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;
                        Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    if (b) {
                        listData.get(position).isselected = true;
                    } else {
                        listData.get(position).isselected = false;
                    }
                }
            }
        });

        holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
        return convertView;
    }

    public boolean checkMaxLimit() {
        int countermax = 0;
        for(ModelPooja item : listData){
            if(item.isselected){
                countermax++;
            }
        }
        return countermax >= 5;
    }

    public class ViewHolder {
        TextView tv;
        public CheckBox checks;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.