How to Change the Text of a Checkbox in Listview

How to change the text of a CheckBox in listview?

Your Custom Adapter must implement CompoundButton.OnCheckedChangeListener

Then

     cb.setChecked(mCheckStates.get(position, false)); 
cb.setOnCheckedChangeListener(this);

Then use the checked state to set text to check box

      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);

}

Example

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<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);

}

}

}

Snap Shot. When you check the checkbox it set's the text when you uncheck set the text to ""

Sample Image

how to check the check box in ListView and change the text in same row in listView?

in the getView method of your ListView adapter:

if(yourCheckBox.isChecked)
yourTextView.setText("text if checkBox is checked.");
else
yourTextView.setText("text if checkBox is not checked.");

ListView with text and checkbox

Try the following:

Demo10.class:----

public class Demo10 extends AppCompatActivity implements Listener {

private ListView lv;
private boolean[] state_ = new boolean[4];
private String[] text_ = new String[4];

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo11);

for(int i = 0 ; i<4 ; i++) {
state_[i] = false;
text_[i] = "text"+i;
}

lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new ItemAdapter(Demo10.this , this));// This includes a Listener that listens to onCheckBoxStateChanges.

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { // used to handle ListView onItemClicks and how they should affect the corresponding checkBox
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

CheckBox cb = view.findViewById(R.id.cb);
cb.setChecked(!cb.isChecked());
displayCheckBoxStateText();
}
});

}

private void displayCheckBoxStateText(){

for(int i = 0 ; i<4 ; i++){
Log.e("CheckBox", text_[i]+" : " + state_[i]);
}
}

@Override
public void checkBox1(boolean state) {
state_[0] = state;
displayCheckBoxStateText();

}

@Override
public void checkBox2(boolean state) {
state_[1] = state;
displayCheckBoxStateText();

}

@Override
public void checkBox3(boolean state) {
state_[2] = state;
displayCheckBoxStateText();

}

@Override
public void checkBox4(boolean state) {
state_[3] = state;
displayCheckBoxStateText();

}
}

ItemAdapter.class:-----

public class ItemAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater layoutInflater;
private Listener callback;

public ItemAdapter(Context c , Listener l) {
mContext = c;
callback = l;
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
return s.length;
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
CheckBox cb;
View view = convertView;
if (convertView == null) {
if (layoutInflater != null) {
view = layoutInflater.inflate(R.layout.demo10, null);
}
}
cb = view.findViewById(R.id.cb);

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (position) {
case 0:
callback.checkBox1(isChecked);
break;
case 1:
callback.checkBox2(isChecked);
break;
case 2:
callback.checkBox3(isChecked);
break;
case 3:
callback.checkBox4(isChecked);
break;
default:
break;
}
}
});
cb.setText(s[position]);
return view;
}

private String[] s = {
"test1",
"test2",
"test3",
"test4"
};

}

Listener interface:----

public interface Listener {

public void checkBox1(boolean state);

public void checkBox2(boolean state);

public void checkBox3(boolean state);

public void checkBox4(boolean state);

}

demo10.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="wrap_content"
android:orientation="vertical"
android:padding="6dip" >

<CheckBox
android:id="@+id/cb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="" />

</RelativeLayout>

demo11.xml:-----

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

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

</android.support.constraint.ConstraintLayout>

how to update the textview text depending on the state of a checkbox in listview

istesd use

holder.ckbox.setText(checkedItems[position]?"Added","Add");

and remove

holder.tvckboxText.setText(ckboxTextAdd[position]?"Added","Add"););

this because it might not support the landscape view

onClick to edit CheckBox data on ListView

Make a public class utils and in it create a public static ArrayList<Integer> positions. And in adapter of listview add setOnClickListener on CheckBox that when it is clicked and checked you add that position to public static array.

public class Utils {
public static ArrayList<Integer> positions =new ArrayList<>();
}

In getView() Function of your adapter after inflating layout add this

CheckBox cBox= (CheckBox) view.findViewById(R.id.myCheckBox); // view is you view inflated
final int myPos= position; //position of cuurent item will be passed into getwiew function

cBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox temp=(CheckBox)v;
if(temp.isChecked()){
Utils.positions.add(myPos);
}
else{
Utils.positions.remove(myPos);
}
}
});

In this way you can get all list of indexes in Utils.postions which are checked when you press button like edit delete etc. AND on click on these buttons you perform your required task on these indexes of main array passed to adapter

Make sure you clear that public static Arraylist everytime you populate list view YOU can do this by calling

Utils.positions.clear()

before notifying adapter everytime.



Related Topics



Leave a reply



Submit