I have been racking my brain and scouring many posts, but can't seem to find the answer to the dreaded state of the Expandable ListView/Checkbox problems. I can't get this to save the state. With ListView, I created a ListArray which would add and remove the position accordingly. I thought that the way around this would be to create a HashMap to do exactly the same, but it doesn't work.
Does anyone have an example of this working and the state of the checkboxes being saved?
Here is it working in ListView, but I need something similar with a HashMap -
private ListArray<Integer> checkedState = new ListArray<Integer>();
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checkedState.add(childPosition);
holder.checkbox.setSelected(true);
//Toast.makeText(_context,holder.txtListChild.getText() + " Selected",Toast.LENGTH_SHORT).show();
}
else{
holder.checkbox.setSelected(false);
checkedState.remove(childPosition).
//Toast.makeText(_context,holder.txtListChild.getText() + " UnSelected",Toast.LENGTH_SHORT).show();
}
}
});
if(checkedState.contains((Integer)childposition) {
holder.checkbox.setChecked(true);
} else {
holder.checkbox.setChecked(false);
}
I ended up not using setOnCheckedChangeListener and using onClick which works great.
The answer can be found here - Expandable listview with selectall checkbox : group itemclick and scrolling bug
Related
I have a custom ListView. It's populated by cursor adapter. It's every row includes one ImageView, two TextViews and one ToggleButton. These all loads fine. When I change state of group of ToggleButtons to true, then when I scroll fast listView not scrolling smoothly it hangs for a sec and scrolls. It happens when after group of unchecked buttons appears group of checked buttons and vice versa. Why list view hangs while scrolling?
Any help would be appreciated. My code looks like this:
public void bindView(View view, Context context, final Cursor cursor) {
final ViewHolder viewHolder = new ViewHolder(view);
(...)
if (viewHolder.toggle_artist != null) {
viewHolder.toggle_artist.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int favourite = isChecked ? 1 : 0;
ContentValues value = new ContentValues();
String where = AmdmContract.ArtistEntry.TABLE_NAME + "." +
AmdmContract.ArtistEntry._ID + " = " + tableId;
value.put(AmdmContract.ArtistEntry.COLUMN_FAVORITE, favourite);
mContext.getContentResolver()
.update(AmdmContract.ArtistEntry.CONTENT_URI,
value, where, null);
}
});
int favFromDB = cursor.getInt(COL_FAVOURITE);
viewHolder.toggle_artist.setChecked(favFromDB == 1);
}
}
Can you please try below code for Toggle Button CheckChangeListener.
Reason: In BaseAdapter getView() method calls on every scroll of ListView and it will calls onCheckChangeListener() automatically.
So replace your code with below code:
tgl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ToggleButton selectedToggle = (ToggleButton) v;
if(selectedToggle.isChecked()){
// Code if toggle is checked
}else{
// Code if toggle is not checked
}
}
});
Hope it will help you.
It is a common problem when you show checkboxex or toggle button in listview items. Once you checked few checkboxes and then scroll down the listview, after scrolling up again it all gets mixed up.
It is advised to use maintain a boolean array which can track the checkbox status.
Check this already answered post - Custom listview with checkbox problem
I have 45 check boxes that you can check. What I want to do is if you try to select more than 6 check boxes it will automatically disable all the buttons, however, if you tap the even one of the checked checkbox ,it will make all the checkbox checkable. This sounds simple ,but I cannot implements this method. I would be grateful if the pros here can help a noob like me. Here is the sample code.
checkbox[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) { buttonView.setTextColor(Color.GREEN);
buttonClicked.add(buttonView.getText().toString());
buttonView.setTextSize(18);
count+=1;
if(count>=6){
for(int i = 0; i< 43;i++){
checkbox[i].setEnabled(false);
stopped = checkbox[i].isChecked();
if(stopped==true){
for(int a = 0; a < checkbox.length;a++){
checkbox[a].setEnabled(true);
}
}
}
}
}
if (!isChecked) {buttonView.setTextColor(Color.BLACK);
buttonClicked.remove(buttonView.getText().toString());
buttonView.setTextSize(15);
count-=1;
Your issue here is your reaction to finding a checked checkbox. We need to look at removing the internal loop under if(stopped==true)(Note 2).
You simply need
if(stopped){
checkbox[i].setEnabled(true);
}
Then in your if(!isChecked)(Note 3) you add your loop back in to reenable all the checkboxes so it will look like
if(!isChecked){
//your existing code
for(int i=0;i<checkbox.length;i++){
checkbox[i].setEnabled(true);
}
}
Note 1: I would advise that you swap your hardcoded "43"to checkbox.length just to keep things cleaner.
Note 2: You don't need to put ==true, it's already a boolean so this can just be if(stopped)
Note 3: This is what "else" was designed for. if(...){} if(!...){} is synonymous with if(...){}else{}.
Note 4: To void unnecessary looping (always good practice) we should maybe add another check here before the for loop to ensure that there were 6 boxes active.
if(count>=6){
for(int i=0;i<checkbox.length;i++){
checkbox[i].setEnabled(true);
}
}
count--;
Note 5: x+=1; can be replaced by x++; and similarly for x-=1; x--;(as said in the comments to your question)
i have apk where on start i show one layout and on the top of is radio buttonst which should on check change layout its something like on start is shown layout PC and on click it should change to Playstation or Xbox
in the evening i will post source code, but now how to make it work ? i found few solutions but nothing work
Why not create new actitivies for different layout files? However try this maybe it will help you.
CB_Event.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
setContentView(R.layout.YOURfirstLayout);
} else {
setContentView(R.layout.YOURSecondLayout);
}
}
});
In this example i used CheckBox you can use RadioButton too.
In my project I created an arraylist to store selected checkboxes values. i.e If a checkbox is selected I am storing the id value of it in the arraylist and if it is unselected I am deleting that from the arraylist. Also if no checkbox is selected or unselected in that page I am storing "00" in the arraylist. The if and elseif conditions are working fine in my code but the *else part is not working..*Where I went wrong? Please help me regarding this....
My Code:
public static ArrayList<String> selchkboxlist = new ArrayList<String>();
cbs[k].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
selchkboxlist.add(Integer.toString(v.getId()));
System.out.println("chkboxlist size" + selchkboxlist.size());
System.out.println("selected checkboxes" + selchkboxlist);
}
else if(!(((CheckBox) v).isChecked()))
{
selchkboxlist.remove(Integer.toString(v
.getId()));
System.out.println("after delete chkboxlist size" + selchkboxlist.size());
System.out.println("un selected checkboxes" + selchkboxlist);
}
else
{
selchkboxlist.add("00");
System.out.println("nothing selected selected checkboxes"
+ selchkboxlist);
}
}
});
The else case will never be entered since a checkbox is either checked, or not checked.
The onclick listener is not called until you actually click on the checkbox.
I am not entirely sure what you want to accomplish, but you might want to add only checked checkboxes to a list of checked items.
Checkboxes can only be checked or not checked, the else statment is unreachable in your code sample.
Use the OnCheckedChangeListener instead of on click:
selchkboxlist.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
//user checked checkbox
}else{
//user unchecked checkbox
}
}
});
Then when "submitting" your checkbox do this check in your loop to see if user has not selected the box
if(!checkbox.isChecked())
selchkboxlist.add("00");
friends. I'm having a really silly problem with radiogroup. Yet I'm unable to find solution.
I will try to describe how to reproduce my problem:
I have radiobutton group and two buttons inside.
I select one of them, lets say 1st one.
Then I'm clearing selection by calling radioGroup.clearCheck()
After I'm trying to select 1st button, but its not checking.
If I check 2nd, it checks normally.
If I check 1st after checking 2nd it also works normally.
This may sound crazy, yet I can't fix it. Please help me, thanks in advance.
I use
#Override
protected void init() {
View view = View
.inflate(getContext(), R.layout.wo_task_yn_result, null);
performed = (RadioButton) view.findViewById(R.id.yn_yes);
notPerformed = (RadioButton) view.findViewById(R.id.yn_no);
radioGroup = (RadioGroup) view.findViewById(R.id.yn_options);
performed.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(final CompoundButton buttonView,
final boolean isChecked) {
Log.d(YES, "verify");
if (isChecked) {
Log.d(YES, "checked");
result = YES;
}
}
});
notPerformed.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(final CompoundButton buttonView,
final boolean isChecked) {
Log.d(NO, "verify");
if (isChecked) {
Log.d(NO, "checked");
result = NO;
}
}
});
addView(view);
}
To create buttons and
#Override
public void clear() {
radioGroup.clearCheck();
result = "";
}
for clearing them
I had the same problem. Was unable to select the first radioButton in the group, but could select the other two. And after selecting the second radioButton, I could select the first one as well. I resolved it by doing a single radioGroup.clearCheck() instead of individual radioButtonA.setChecked(false)
Use OnCheckedChangeListener and make use of the method setSelected(true) or setChecked(true).
if u want to uncheck radio button try this method:
#Override
public void clear() {
performed.setChecked(false);
nonperformed.setChecked(false);
}