In my listview I have 3 textviews and I want to hide one textview at some positions.
The positions at which TextView must hide comes at runtime based on some condition.
If I get the indexes I want to hide, in an array and use this logic,then it hides the TextView only for the last index. please help.
int []x={1,2,4};
for(int i=0;i<x.length;i++){
if(position==x[i]){
holder.time.setVisibility(View.GONE);
}
else{
holder.time.setVisibility(View.VISIBLE);
}
}
Here
if(position==x[i]){
holder.time.setVisibility(View.GONE);
}
else{
holder.time.setVisibility(View.VISIBLE);
}
change this to
if(position==x[i]){
holder.time.setVisibility(View.GONE);
break;
}
else{
holder.time.setVisibility(View.VISIBLE);
}
OR
do something like this
int []x={1,2,4};
boolean exists = false;
for(int i=0;i<x.length;i++)
{
if(position==x[i]){
exists = true;
break;
}
}
if (exists)
holder.time.setVisibility(View.GONE);
else
holder.time.setVisibility(View.VISIBLE);
Try using OR condition, instead of for loop.
if (position == 1 || position == 2 || position == 4)
{
holder.time.setVisibility(View.GONE);
}
else
{
holder.time.setVisibility(View.VISIBLE);
}
EDIT --- You can try this way also
int []x={1,2,3,4};
boolean hideMe ;
for(int i = 0; i < x.length; i++)
{
hideMe = false;
if(position == x[i])
{
hideMe = true;
break;
}
}
if (hideMe)
holder.time.setVisibility(View.GONE);
else
holder.time.setVisibility(View.VISIBLE);
Related
I have four radio buttons in a radio group in my MCQ app. When I select a radio button and go to next question, there is a problem if I select same radio button as previous, the radio button automatically deselect, but after selecting any other button, it is working.
private int getSelectedAnswer(int radioSelected){
int answerSelected = 0;
if(radioSelected == R.id.radio0){
answerSelected = 1;
}
if(radioSelected == R.id.radio1){
answerSelected = 2;
}
if(radioSelected == R.id.radio2){
answerSelected = 3;
}
if(radioSelected == R.id.radio3){
answerSelected = 4;
}
return answerSelected;
}
private void selectedRadioButton(int ansSelected){
if(ansSelected == 1){
optionOne.setChecked(true);
}
if(ansSelected == 2){
optionTwo.setChecked(true);
}
if(ansSelected == 3){
optionThree.setChecked(true);
}
if(ansSelected == 4){
optionFour.setChecked(true);
}
}
private void uncheckedRadioButton(){
optionOne.setChecked(false);
optionTwo.setChecked(false);
optionThree.setChecked(false);
optionFour.setChecked(false);
}
private void showQuestions(){
if(currentQuizQuestion >= quizCount){
currentQuizQuestion=currentQuizQuestion-1;
Toast.makeText(ShowSingleQuestionsOnline.this, "End of the Quiz Questions", Toast.LENGTH_LONG).show();
return;
}
else {
uncheckedRadioButton();
quizQuestion.setText(1+ currentQuizQuestion + " : " + MyQuestArrList.get(currentQuizQuestion).get("QuestName"));
int dd=Integer.parseInt(MyQuestArrList.get(currentQuizQuestion).get("QueType"),10);
optionOne.setText(MyQuestArrList.get(currentQuizQuestion).get("QueOption1"));
optionTwo.setText(MyQuestArrList.get(currentQuizQuestion).get("QueOption2"));
optionThree.setText(MyQuestArrList.get(currentQuizQuestion).get("QueOption3"));
optionFour.setText(MyQuestArrList.get(currentQuizQuestion).get("QueOption4"));
}
}
Thanks in advance.
This behavior is normal because the radio option state is not reset.
Take a look on your getSelectedAnswer() method :
private int getSelectedAnswer(int radioSelected){
int answerSelected = 0;
if(radioSelected == R.id.radio0){
answerSelected = 1;
}
if(radioSelected == R.id.radio1){
answerSelected = 2;
}
if(radioSelected == R.id.radio2){
answerSelected = 3;
}
if(radioSelected == R.id.radio3){
answerSelected = 4;
}
return answerSelected;
}
When you go to the next question, the selected radio option state remain and when you click again on it, it value change (deselect).
To avoid this, you need to reset your radio options before go to the next question.
Hey i am writing a game when there are bunch of textures that changes locations, but i want to check that they are not drawing over other textures, i tried to write a code for checking it but its not working well.
Here is the code i tried:
circles.setPosition(new Vector2(r.nextInt(width-height/8*2)+height/8,
r.nextInt(height-height/8*2)+height/8),
i);
circl[i].set((float) (circles.getPosition(i).x+height/16),
(float) (circles.getPosition(i).y+height/16),
height/16);
while(isLaping = true){
System.out.println("in");
for(int y = 0; y < circlesArray.length-1; y++){
if(Intersector.overlaps(circl[y], circl[y+1])){
circles.setPosition(new Vector2(r.nextInt(width-height/8*2)+height/8,
r.nextInt(height-height/8*2)+height/8),
i);
circl[i].set((float) (circles.getPosition(i).x+height/16),
(float) (circles.getPosition(i).y+height/16),
height/16);
}else{
isLaping = false;
}
}
}
How to fix it?
this is confusing,
while(isLaping = true){
you mean it
while(isLaping == true){
or
while(isLaping){
while(isLaping=true); isLaping never is false at the moment evaluate while because asign true every time
I tried again to do so with your tip but the circle are getting crazy and changes places every second
this is the code i have been trying.
for(int i = 0;i<circlesArray.length;i++)
{
if(circlesArray[i]>200)
{
changePosition(i);
}
circlesArray[i]++;
}
private void changePosition(int i)
{
int s = 0;
circles.setPosition(new Vector2(r.nextInt(width-height/12*2)+height/8,r.nextInt(height-height/12*2)+height/12),i);
circl[i].set((float) (circles.getPosition(i).x+height/24),(float) (circles.getPosition(i).y+height/24),height/24);
while (lap == true)
{
circles.setPosition(new Vector2(r.nextInt(width-height/12*2)+height/8,r.nextInt(height-height/12*2)+height/12),i);
circl[i].set((float) (circles.getPosition(i).x+height/24),(float) (circles.getPosition(i).y+height/24),height/24);
for(int y= i+1;y<circlesArray.length;y++)
{
if(circl[i].overlaps(circl[y]))
{
circles.setPosition(new Vector2(r.nextInt(width-height/12*2)+height/8,r.nextInt(height-height/12*2)+height/12),i);
circl[i].set((float) (circles.getPosition(i).x+height/24),(float) (circles.getPosition(i).y+height/24),height/24);
s=0;
}
else
{
s++;
}
}
if(s == circlesArray.length)
lap = false;
}
circlesArray[i] = 0;
x[i] = r.nextInt(circleTexture.length);
}
I've tried everything from matching strings, to using TextUtils.isEmpty. No matter what I do, b is always true (even when edittext is purposely left blank) which allows the code to proceed to the next steps (this is a Madlib app).
If anybody can see why the code isn't properly checking for blank edittext's and displaying the Please Fill In All Fields" toast when one is blank, it would be very appreciated. Thanks. Sorry for the messy code.
public class Madlibs extends Fragment {
switch (((MainActivity) getActivity()).getStory()) {
case 0:
output.setText(Stories[0]);
title = Titles[0];
actionBar.setTitle(title);
editTextNumber = 12;
addEdit = new BootstrapEditText[editTextNumber];
for (int i = 0; i < addEdit.length; i++) {
addEdit[i] = new BootstrapEditText(getActivity());
l_layout.addView(addEdit[i]);
params.setMargins(0, 20, 0, 20);
addEdit[i].setLayoutParams(params);
addEdit[i].setId(i);
}
addEdit[0].setHint("Name of Sickness");
addEdit[1].setHint("Adjective");
addEdit[2].setHint("Name of Boy");
addEdit[3].setHint("Body Part");
addEdit[4].setHint("Color");
addEdit[5].setHint("Animal");
addEdit[6].setHint("Article of Clothing");
addEdit[7].setHint("Relative");
addEdit[8].setHint("Adjective");
addEdit[9].setHint("Article of Clothing");
addEdit[10].setHint("Body Part");
addEdit[11].setHint("Number");
break;
case 1:
// fragment = new Madlibs();
break;
case 2:
// fragment = new MadlibsSaved();
}
convert = (BootstrapButton) view.findViewById(R.id.convert);
convert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i;
String s;
for (i = 0; i < addEdit.length; i++) {
s = addEdit[i].getText().toString().trim();
if (s.isEmpty() || s.length() == 0 || s.equals("") || s == null) {
b = false;
}
else
{
b = true;
}
}
if (b = true) {
gather();
postIt();
outputText = output.getText().toString();
} else {
Toast.makeText(getActivity().getApplicationContext(),
"Please Fill In All Fields", Toast.LENGTH_LONG)
.show();
}
}
});
Your problem is here
if (b = true) { // HERE
gather();
postIt();
outputText = output.getText().toString();
}
You are using b = true which sets b to true, and since you are able to do this successfully, the conditional evaluates to true every time. What you want instead is the comparison operator ==
if (b == true) {
gather();
postIt();
outputText = output.getText().toString();
}
or even better, since the variable you are comparing is a boolean, you could just use
if (b) {} //This is "if b is true..."
if (!b) {} //This is "if b is false..."
You can see another one of my answers about this here.
for (i = 0; i < addEdit.length; i++) {
s = addEdit[i].getText().toString().trim();
if (s.isEmpty() || s.length() == 0 || s.equals("") || s == null) {
b = false;
}
else {
b = true;
}
}
For Loop ends here and the following is outside the For Loop :
if (b = true) {
gather();
postIt();
outputText = output.getText().toString();
} else {
Toast.makeText(getActivity().getApplicationContext(),
"Please Fill In All Fields", Toast.LENGTH_LONG)
.show();
}
Problem 1 :
if(b == true) {
}
Problem 2 :
Now, suppose edittext 1 is empty, b is set to true but then it'll straightaway move ahead in the loop to edittext 2 to the last edittext which are not empty hence it'll set b to false again. You'll need to put the check inside the for loop.
I can't seem to figure out how to write this code more efficiently. I'm iterating through views to check validity (text entered) but i find myself casting way too much. According to eclipse i need to cast in order to access the methods on the view. Here's the code:
// Verify Drivers/Vehicles Entered
private boolean checkDriversVehiclesValidity() {
int viewCount = mContainerView.getChildCount();
for (int i = 0; i < viewCount; i++) {
View v = mContainerView.getChildAt(i);
if (v.getId() == R.id.driverVehicleRow) {
for (int j = 0; j < ((LinearLayout) v).getChildCount(); j++) {
View v1 = ((ViewGroup) v).getChildAt(j);
if (v1 instanceof CustomAutoCompleteTextView) {
if (((CustomAutoCompleteTextView) v1).getError() != null) {
v1.requestFocus();
return false;
}
if (v1.getId() == R.id.drivers_field) {
String driverNumber = ((CustomAutoCompleteTextView) v1).getText().toString();
if ("".equals(driverNumber)) {
((CustomAutoCompleteTextView) v1).setError("Driver required");
v1.requestFocus();
return false;
}
} else if (v1.getId() == R.id.vehicles_field) {
String vehicleNumber = ((CustomAutoCompleteTextView) v1).getText().toString();
if ("".equals(vehicleNumber)) {
((CustomAutoCompleteTextView) v1).setError("Vehicle required");
v1.requestFocus();
return false;
}
}
}
}
}
}
return true;
}
For example, after checking for
if (v1 instanceof CustomAutoCompleteTextView)
you can be sure it IS an instance of CustomAutoCompleteTextView, so you can assign it to a properly typed variable like this:
CustomAutoCompleteTextView cv = (CustomAutoCompleteTextView)v1;
and use cv instead of ((CustomAutoCompleteTextView) v1) later.
I'm trying to get an indexable list in my list view. I referred this. But while using the code, I'm facing with an error while using Korean Characters in the StringMatcher class. Can anyone explain me the usage of this class? Is this class required for English Characters as well?
Thanks in advance.
There are some changes to be done to make it work. In order to compile the project and get rid of korean text update the StringMatcher class
package com.woozzu.android.util;
public class StringMatcher {
public static boolean match(String value, String keyword) {
if (value == null || keyword == null)
return false;
if (keyword.length() > value.length())
return false;
int i = 0, j = 0;
do {
int vi = value.charAt(i);
int kj = keyword.charAt(j);
if (isKorean(vi) && isInitialSound(kj)) {
} else {
if (vi == kj) {
i++;
j++;
} else if (j > 0)
break;
else
i++;
}
} while (i < value.length() && j < keyword.length());
return (j == keyword.length())? true : false;
}
private static boolean isKorean(int i) {
return false;
}
private static boolean isInitialSound(int i) {
return false;
}
}