Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your Custom Adapter must implement <code>CompoundButton.OnCheckedChangeListener</code></p> <p>Then</p> <pre><code> cb.setChecked(mCheckStates.get(position, false)); cb.setOnCheckedChangeListener(this); </code></pre> <p>Then use the checked state to set text to check box</p> <pre><code> public boolean isChecked(int position) { return mCheckStates.get(position, false); } public void setChecked(int position, boolean isChecked) { mCheckStates.put(position, isChecked); } public void toggle(int position) { setChecked(position, !isChecked(position)); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { buttonView.setText("Hello"); } else { buttonView.setText(""); } mCheckStates.put((Integer) buttonView.getTag(), isChecked); } </code></pre> <p>Example</p> <pre><code>public class MainActivity extends Activity implements AdapterView.OnItemClickListener { int count; private CheckBoxAdapter mCheckBoxAdapter; String[] GENRES = new String[] { "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama", "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ListView listView = (ListView) findViewById(R.id.lv); listView.setItemsCanFocus(false); listView.setTextFilterEnabled(true); listView.setOnItemClickListener(this); mCheckBoxAdapter = new CheckBoxAdapter(this, GENRES); listView.setAdapter(mCheckBoxAdapter); Button b= (Button) findViewById(R.id.button1); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub StringBuilder result = new StringBuilder(); for(int i=0;i&lt;GENRES.length;i++) { if(mCheckBoxAdapter.mCheckStates.get(i)==true) { result.append(GENRES[i]); result.append("\n"); } } Toast.makeText(MainActivity.this, result, 1000).show(); } }); } public void onItemClick(AdapterView parent, View view, int position, long id) { mCheckBoxAdapter.toggle(position); } class CheckBoxAdapter extends ArrayAdapter implements CompoundButton.OnCheckedChangeListener { private SparseBooleanArray mCheckStates; LayoutInflater mInflater; TextView tv1,tv; CheckBox cb; String[] gen; CheckBoxAdapter(MainActivity context, String[] genres) { super(context,0,genres); mCheckStates = new SparseBooleanArray(genres.length); mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); gen= genres; } @Override public int getCount() { // TODO Auto-generated method stub return gen.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View vi=convertView; if(convertView==null) vi = mInflater.inflate(R.layout.checkbox, null); tv= (TextView) vi.findViewById(R.id.textView1); cb = (CheckBox) vi.findViewById(R.id.checkBox1); tv.setText("Name :"+ gen [position]); cb.setTag(position); cb.setChecked(mCheckStates.get(position, false)); cb.setOnCheckedChangeListener(this); return vi; } public boolean isChecked(int position) { return mCheckStates.get(position, false); } public void setChecked(int position, boolean isChecked) { mCheckStates.put(position, isChecked); } public void toggle(int position) { setChecked(position, !isChecked(position)); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { buttonView.setText("Hello"); } else { buttonView.setText(""); } mCheckStates.put((Integer) buttonView.getTag(), isChecked); } } } </code></pre> <p>Snap Shot. When you check the checkbox it set's the text when you uncheck set the text to ""</p> <p><img src="https://i.stack.imgur.com/THdnU.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload