Android: If statement based on checkbox not being checked - android

I think this is really easy but I am fairly new to android development so thanks in advance
If the first one is checked I generate a random number for text Item 1. If both the checkboxs are checked I generate text for the second text. I was wondering what is the most efficient way to do this
Here is the snippet that I need help with
if(edit1.isChecked()){
text1.setText(String1[randomInt]);
}
if(edit1.isChecked() && edit2.isChecked()){
text2.setText(String1[randomInt]);
}
Obviously, the first statement will show true in both. basically is there a way to say if edit2 is false?

Try this:
edit2.isChecked()=false;
if(edit1.isChecked() && (edit2.isChecked()==false)) {
text2.setText(String1[randomInt]);
}

#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v.findViewById(R.id.chkSelected);
TextView tv = (TextView) v.findViewById(R.id.tvName);
// pi = (PackageInfo) arg0.getItemAtPosition(position);
cb.performClick();
if (cb.isChecked()) {
studentList.add(tv.getText().toString());
} else if (!cb.isChecked()) {
studentList.remove(tv.getText().toString());
}
}

Related

disabled checkboxes get enabled radomly after recycling view through the adapter in android app

i have the following problem. i have set three checkboxes in my app in a way so when i check one of them the other two get disabled. the problem is that when i scroll up or down namely when the views get recycled some of the disabled checkboxes get enabled which of course i dont want to happen. could you please help me to solve that problem?
the code in the adapter is this :
#NonNull
#Override
public View getView(final int position1, View convertView1, ViewGroup parent1) {
View listItemView1 = convertView1;
if (listItemView1 == null) {
listItemView1 = LayoutInflater.from(getContext()).inflate(R.layout.list_item1, parent1, false);
}
final ColorQuiz currentColorQuiz = getItem(position1);
TextView questionTextView = (TextView) listItemView1.findViewById(question_text_view);
questionTextView.setText(currentColorQuiz.getQuestionHeader());
final CheckBox box1 = (CheckBox) listItemView1.findViewById(R.id.check_Box_view1);
box1.setText(currentColorQuiz.getCheckBoxTextA());
final CheckBox box2 = (CheckBox) listItemView1.findViewById(R.id.check_Box_view2);
box2.setText(currentColorQuiz.getCheckBoxTextB());
final CheckBox box3 = (CheckBox) listItemView1.findViewById(R.id.check_Box_view3);
box3.setText(currentColorQuiz.getCheckBoxTextC());
box1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (box1.isChecked() ) {
thesisA[position1]=true;
box2.setEnabled(false);
box3.setEnabled(false);
} else {
thesisA[position1]=false;
box2.setEnabled(true);
box3.setEnabled(true);
}
int getPositionA = (Integer) compoundButton.getTag(); // Here we get the position that we have set for the checkbox using setTag.
colorQuizs.get(getPositionA).setSelectedA(compoundButton.isChecked());
}
});
box1.setTag(position1);
box1.setChecked(colorQuizs.get(position1).isSelectedA());
.
.
.
and continues the same way with the next two checkboxes (box2, box3).
maybe while you calling box1.setChecked then one of the old listeners is called and it is causing problems (as it still has old tag, and propably setting wrong data to your model ).
Try to add at the beggining to make sure that something wrong does not happen:
box1.setOnCheckedChangeListener(null);
box2.setOnCheckedChangeListener(null);
box3.setOnCheckedChangeListener(null);
This will tell you if not calling callback while property does not change is reason of this problem:
box1.setChecked(!colorQuizs.get(position1).isSelectedA());
box1.setChecked(colorQuizs.get(position1).isSelectedA());

Getting String from Spinner, and using it on conditional statements

I have this program that will change the hours and minutes of the values I get from Calendar.
So I'm changing only the hour, I'm doing a Timezone thing here. So, what I do is I make an array of the TimeZones at Strings.xml and put it on a spinner. And then, when I change the item on the spinner, I set the text of a textview to the selected value on the spinner.
I can do it up to here.
My problem lies in the conditional statements. I have a button that gets the text in the TextView and I will use that in my If statements. This is my Syntax.
This gets me the values from the Spinner to the TextView.
Spinner TimezoneSelect = (Spinner)findViewById (R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.timzones, R.layout.support_simple_spinner_dropdown_item);
TimezoneSelect.setAdapter(adapter);
//final String SelectedTimeZone = TimezoneSelect.getSelectedItem().toString();
TimezoneSelect.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
TimeZoneStatus = parent.getItemAtPosition(position).toString();
TimeZoneDisplay.setText(TimeZoneStatus);
And this is the faulty If statement.
public void onClick(View v) {
// TODO Auto-generated method stub
int newhour;
String TimeZoneNow = TimeZoneStatus.trim().toString();
String Jakarta = "UTC+7:00 (Jakarta)";
if ((TimeZoneNow == "UTC+7:00(Jakarta)") || (TimeZoneNow == Jakarta))
//^lol desperate code
{
newhour = hour - 1;
TimeText.setText(newhour + ":" + minutes);
}
}
});
Help! :c
try this way
if(TimeZoneNow.equals(Jakarta)
used .equals() method for string comparison
use this code
if ((TimeZoneNow .equalsIgnoreCase(Jakarta))
//^lol desperate code
{
newhour = hour - 1;
TimeText.setText(newhour + ":" + minutes);
}
Try this:
In your case replace == with eqauls().
Explanation:
== operator is used to compare reference.
equals() is used to compare content.

Android Gallery visible ImageView is null?

I want to draw a check mark for the image view I click on and uncheck the imageview I clicked on before using the following code snip. I store last checked position in mDeviceAdapter. When I try to uncheck old position, the image view always gives null even for the partial visible image view. I am really confused because I thought only invisible one is recycled... Newbie in Android and any comment is appreciated.
public void CheckableImageView#setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
invalidate();
}
}
mDeviceGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CheckableImageView viewToCheck = (CheckableImageView) view;
if (!viewToCheck.isChecked()) {
int oldCheckedPosition = mDeviceAdapter
.getCheckedPosition();
mDeviceAdapter.setCheckedPosition(position);
View checkedView = mDeviceGallery
.getChildAt(oldCheckedPosition);
Log.d(TAG, "old position="+oldCheckedPosition + "old view="+checkedView);
if (checkedView != null) {
((CheckableImageView) checkedView)
.setChecked(false);
Log.d(TAG, "uncheck position="
+ oldCheckedPosition);
}
viewToCheck.setChecked(true);
That's not the right approach.
You need to add to your data type a boolean field (i.e mIsChecked).
On the onItemClick method set the value of that variable to true and keep its INDEX as a member of the adapter. When another item is clicked set the value of that item to true and set the value of the saved one to false (change the value of the datatype in you ArrayList in the INDEX you stored in the previous click).
Now, in the getView() method, you must have if/else statement. Something like:
if (item.isChecked())
{
checkedView.setChecked(true);
}
else
{
checkedView.setChecked(false);
}
Example to the onClick method: (just a general direction)
if (item.isChecked())
{
checkedView.setChecked(false);
yourList.get(position).setChecked(true);
yourList.get(mLastCheckedIndex).setChecked(false);
mLastCheckedIndex = position;
notifyDataSetChanged();
}
else
{
//same but opposite.
}
Hope this helps!

Last item click null pointer excepction

listView=(ListView)findViewById(R.id.list);
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View viewItem, int position, long arg3) {
if(!itemClicked)
{
viewItem = parent.getChildAt(position);
((Button)viewItem.findViewById(R.id.gov)).setVisibility(View.VISIBLE);
((Button)viewItem.findViewById(R.id.re)).setVisibility(View.VISIBLE);
viewItem.invalidate();
itemClicked=true;
clickedItemPos=position;
TextView text = (TextView)viewItem.findViewById(R.id.item);
ime = text.getText();
}
else
{
viewItem=parent.getChildAt(clickedItemPos);
((Button)viewItem.findViewById(R.id.go)).setVisibility(View.INVISIBLE);
((Button)viewItem.findViewById(R.id.re)).setVisibility(View.INVISIBLE);
viewItem = parent.getChildAt(position);
((Button)viewItem.findViewById(R.id.go)).setVisibility(View.VISIBLE);
((Button)viewItem.findViewById(R.id.re)).setVisibility(View.VISIBLE);
viewItem.invalidate();
clickedItemPos=position;
TextView text = (TextView)viewItem.findViewById(R.id.item);
ime = text.getText();
}
final int[] coordAndCat = FavoriteCoord(ime.toString());
Nullpointer exception happens when I have more elements and when you scroll and click the last item on the listview. How to workaround this?
Note: I'm trying to display 2 buttons in every item that is being clicked. This code works for all clicks on items besides the last one (if there are many elements in the list and you need to scroll)
clickedItemPos isn't defined after else unless clickedItemPos=position has been called earlier. What line does the error happen on?
listView=(ListView)findViewById(R.id.list);
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View viewItem, int position, long arg3) {
if(!itemClicked)
{
viewItem = parent.getChildAt(position);
//start >> This might be where your problem is (R.id.gov)
((Button)viewItem.findViewById(R.id.gov)).setVisibility(View.VISIBLE);
//end <<
((Button)viewItem.findViewById(R.id.re)).setVisibility(View.VISIBLE);
viewItem.invalidate();
itemClicked=true;
clickedItemPos=position;
TextView text = (TextView)viewItem.findViewById(R.id.item);
ime = text.getText();
}
else
{
viewItem=parent.getChildAt(clickedItemPos);
((Button)viewItem.findViewById(R.id.go)).setVisibility(View.INVISIBLE);
((Button)viewItem.findViewById(R.id.re)).setVisibility(View.INVISIBLE);
viewItem = parent.getChildAt(position);
((Button)viewItem.findViewById(R.id.go)).setVisibility(View.VISIBLE);
((Button)viewItem.findViewById(R.id.re)).setVisibility(View.VISIBLE);
viewItem.invalidate();
clickedItemPos=position;
TextView text = (TextView)viewItem.findViewById(R.id.item);
ime = text.getText();
}
final int[] coordAndCat = FavoriteCoord(ime.toString());
I think you said R.id.gov instead of R.id.go unless R.id.gov is truly an id you have. That is the only thing I saw 'wrong', please show a stacktrace for the error next time, it would help in debugging =)
EDIT
is clickedItemPos initialized somewhere else? because if it isn't this line: viewItem=parent.getChildAt(clickedItemPos); in your else statement would probably be null if nothing was selected before. I understand you want to use this in order to check for previously clicked items, but if nothing was clicked before, this will never get set to a value and therefore be null try doing this in that else statement to avoid the possible null variable:
else
{
if(clickedItemPos != null){ //only add this if there was a previous clicked item
viewItem=parent.getChildAt(clickedItemPos);
((Button)viewItem.findViewById(R.id.go)).setVisibility(View.INVISIBLE);
((Button)viewItem.findViewById(R.id.re)).setVisibility(View.INVISIBLE);
viewItem = parent.getChildAt(position);
((Button)viewItem.findViewById(R.id.go)).setVisibility(View.VISIBLE);
((Button)viewItem.findViewById(R.id.re)).setVisibility(View.VISIBLE);
viewItem.invalidate();
clickedItemPos=position;
TextView text = (TextView)viewItem.findViewById(R.id.item);
ime = text.getText();
}else{
viewItem = parent.getChildAt(position);
((Button)viewItem.findViewById(R.id.go)).setVisibility(View.VISIBLE);
((Button)viewItem.findViewById(R.id.re)).setVisibility(View.VISIBLE);
viewItem.invalidate();
clickedItemPos=position;
TextView text = (TextView)viewItem.findViewById(R.id.item);
ime = text.getText();
}
}
hopefully that will fix it. In your stack trace it says line 91 in your class is the line causing the error, what variable is located on that line? that is usually a good indicator of what variable is causing the null pointer, but since we don't know the line numbers for your code it is hard to guess where the issue is.
Yes there is repeated code in that solution but you could always make a method to put the repeated code in and call that instead if you want.
Good Luck, hope this helps.

Checkbox's state not properly shown when `performClick` is used

I got a tricky problem here:
in my app, there's a list (using a ListView) and each item contains text and a checkbox aligned to the right hand side.
Now, clicking the checkbox itself works, I installed a handler (OnClickListener) for it and perform an internal action. The visible state of the checkbox is just as expected - clicked the first time the tick is there, clicked again the tick disappears.
Now I simply wanted to "extend" the clickable area to the complete list entry - my approach was to install an OnItemClickListener using ListView.setOnItemClickListener() and within this listener I do the following:
resultView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
checkBox.performClick();
}
});
That sounds simple, but what happens is that sometimes (not every time) the tick in the checkbox does not appear after that! I added a checkBox.invalidate() but that did not help.
How can I have a checkbox "react" to the click of the complete list entry item???
Thanks!
Rather than use performClick() use checkBox.setChecked( !checkBox.getChecked() ); instead. performClick() will cause the onClickListener to be called again potentially causing a recursion, which could explain the odd behaviour that you're seeing.
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v.findViewById(R.id.chkSelected);
TextView tv = (TextView) v.findViewById(R.id.tvName);
// pi = (PackageInfo) arg0.getItemAtPosition(position);
cb.performClick();
if (cb.isChecked()) {
checkedValue.add(tv.getText().toString());
} else if (!cb.isChecked()) {
checkedValue.remove(tv.getText().toString());
}
}

Categories

Resources