Krishna Veni
Krishna Veni

Reputation: 2257

The checkbox and textview with custom spinner in Android

I am having the 7 items in the spinner box. I have created the spinner box with textview and checkbox. I want to do the following logic.

Can you please give me any suggestion to do:

private static final String[] select_qualification = {
        "Select Qualification", "10th / Below", "12th", "Diploma", "UG",
        "PG", "Phd" };

If I will check the UG checkbox, it will automatically checked state the checkbox for 10th/Below, 12th,Diploma and also disable the checked state of checkbox for PG and phd.

If am checking the any check box means the previous item checkbox automatically need to checked state and next item checkbox automatically need to unchecked state.

Right now this is my adapter file:

public class CustomAdapter extends ArrayAdapter<String> {

    public CustomAdapter(Context context, int resourceId, String[] objects) {
        super(context, resourceId, objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView,
            ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();

        View row = inflater.inflate(R.layout.spinner_item, parent, false);
        final TextView label = (TextView) row
                .findViewById(R.id.spinneritemqualification);
        label.setText(select_qualification[position]);

        final CheckBox checkboxButton = (CheckBox) row
                .findViewById(R.id.checkboxquali);
        if ((position == 0)) {

            checkboxButton.setVisibility(View.INVISIBLE);
        }
        checkboxButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

             @Override
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                 // TODO Auto-generated method stub
                 System.out.println("Position "+ isChecked);
                // Integer pos = (Integer) buttonView.getTag(); 
                 int getPosition = (Integer) buttonView.getTag();  
                 Toast.makeText(getApplicationContext(),
                        "CLICKED:" + getPosition, Toast.LENGTH_LONG)
                        .show();

                 if (isChecked) {
                     checkboxButton.setChecked(true);
                 }
                 else {
                     checkboxButton.setChecked(false);
                 }
             }
         });

Upvotes: 2

Views: 6254

Answers (1)

AndiGeeky
AndiGeeky

Reputation: 11464

I have tried to implement what you want. And here is Demo of it.

You can modify as your requirement.

Custom adapter :

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

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


public class MyAdapter extends ArrayAdapter<StateVO> {
    private Context mContext;
    private ArrayList<StateVO> listState;
    private MyAdapter myAdapter;
    private boolean isFromView = false;

    public MyAdapter(Context context, int resource, List<StateVO> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.listState = (ArrayList<StateVO>) objects;
        this.myAdapter = this;
    }

    @Override
    public View getDropDownView(int position, View convertView,
                                ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(final int position, View convertView,
                              ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            LayoutInflater layoutInflator = LayoutInflater.from(mContext);
            convertView = layoutInflator.inflate(R.layout.spinner_item, null);
            holder = new ViewHolder();
            holder.mTextView = (TextView) convertView
                    .findViewById(R.id.text);
            holder.mCheckBox = (CheckBox) convertView
                    .findViewById(R.id.checkbox);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mTextView.setText(listState.get(position).getTitle());

        // To check weather checked event fire from getview() or user input
        isFromView = true;
        holder.mCheckBox.setChecked(listState.get(position).isSelected());
        isFromView = false;

        if ((position == 0)) {
            holder.mCheckBox.setVisibility(View.INVISIBLE);
        } else {
            holder.mCheckBox.setVisibility(View.VISIBLE);
        }
        holder.mCheckBox.setTag(position);
        holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();

                if (!isFromView) {
                    Log.e("isFromView", !isFromView + "");
                    if (getPosition == 4) { // cam make a rule according to it for any position
                        if (!listState.get(4).isSelected()) {
                            if (isChecked) {
                                for (int i = 0; i < listState.size(); i++) {
                                    if (i < 4) {
                                        listState.get(i).setSelected(true);
                                    } else if (i == 4) {
                                        listState.get(i).setSelected(true);
                                    } else {
                                        listState.get(i).setSelected(false);
                                    }
                                }
                            }
                        } else {
                            if (!isChecked) {
                                for (int i = 0; i < listState.size(); i++) {
                                    if (i < 4) {
                                        listState.get(i).setSelected(false);
                                    } else if (i == 4) {
                                        listState.get(i).setSelected(false);
                                    }
                                }
                            }
                        }
                        myAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
        return convertView;
    }

    private class ViewHolder {
        private TextView mTextView;
        private CheckBox mCheckBox;
    }
}

Basic VO to handle state of checkbox :

public class StateVO {
    private String title;
    private boolean selected;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
}

Spinner item layout :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="text"
        android:textAlignment="gravity" />

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true" />
</RelativeLayout>

Spinner in your activity :

 <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp" />

Method to set data in spinner :

private void setSpinnerData() {
        final String[] select_qualification = {
                "Select Qualification", "10th / Below", "12th", "Diploma", "UG",
                "PG", "Phd"};
        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        ArrayList<StateVO> listVOs = new ArrayList<>();

        for (int i = 0; i < select_qualification.length; i++) {
            StateVO stateVO = new StateVO();
            stateVO.setTitle(select_qualification[i]);
            stateVO.setSelected(false);
            listVOs.add(stateVO);
        }
        MyAdapter myAdapter = new MyAdapter(MainActivity.this, 0,
                listVOs);
        spinner.setAdapter(myAdapter);
    }

Enjoy coding.!!

Upvotes: 5

Related Questions