I have 4 checkboxes, when I check tick one, two or more. I want to populate my spinner with values, 1-5 if checkbox 1 is checked, 6-10 if checkbox 2 is checked, etc.. I have this logic here.
public void populateSpinnerToothNumber() {
if (cbQuadrant1.isChecked()) {
ArrayList<String> toothNumber = new ArrayList<>();
toothNumber.add("1");
toothNumber.add("2");
toothNumber.add("3");
toothNumber.add("4");
toothNumber.add("5");
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
spinnerToothNumber.setAdapter(stringArrayAdapter);
} else if (cbQuadrant2.isChecked()) {
} else if (cbQuadrant3.isChecked()) {
} else if (cbQuadrant4.isChecked()) {
} else if (cbQuadrant1.isChecked() && cbQuadrant2.isChecked()) {
ArrayList<String> toothNumber = new ArrayList<>();
toothNumber.add("1");
toothNumber.add("2");
toothNumber.add("3");
toothNumber.add("4");
toothNumber.add("5");
toothNumber.add("6");
toothNumber.add("7");
toothNumber.add("8");
toothNumber.add("9");
toothNumber.add("10");
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
spinnerToothNumber.setAdapter(stringArrayAdapter);
}
}
How can I improve the logic?
UPDATE
public void populateSpinnerToothNumber() {
final ArrayList<String> toothNumber = new ArrayList<>();
cbQuadrant1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
for (int x = 1; x <= 5; x++) {
toothNumber.add(String.valueOf(x));
}
}
});
cbQuadrant2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
for (int x = 6; x <= 10; x++) {
toothNumber.add(String.valueOf(x));
}
}
});
cbQuadrant3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
for (int x = 11; x <= 15; x++) {
toothNumber.add(String.valueOf(x));
}
}
});
cbQuadrant4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
for (int x = 16; x <= 20; x++) {
toothNumber.add(String.valueOf(x));
}
}
});
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
spinnerToothNumber.setAdapter(stringArrayAdapter);
}
This solved the problem :) Managed it with checkbox.setOnCheckedChangeListener Thanks for helping! :)
Adding to #Nilesh Rathod. You can have a method like called populateSpinner (int bgCount, int Endcount) which takes in the amount of tooth number needed from the beginning range to the end.
private void populateSpinner (int bgCount, int Endcount) {
ArrayList<String> toothNumber = new ArrayList<>();
for (int i = bgCount; i < Endcount; i++) {
toothNumber.add(i);
}
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
spinnerToothNumber.setAdapter(stringArrayAdapter);
}
Call the method like this in your check boxes OnCheckedChangeListener.
populateSpinner (1, 5);
you should use OnCheckedChangeListener
Interface definition for a callback to be invoked when the checked state of a compound button changed.
sample code
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
ArrayList<String> toothNumber = new ArrayList<>();
toothNumber.add("1");
toothNumber.add("2");
toothNumber.add("3");
toothNumber.add("4");
toothNumber.add("5");
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
spinnerToothNumber.setAdapter(stringArrayAdapter);
}
}
}
);
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
ArrayList<String> toothNumber = new ArrayList<>();
toothNumber.add("1");
toothNumber.add("2");
toothNumber.add("3");
toothNumber.add("4");
toothNumber.add("5");
toothNumber.add("6");
toothNumber.add("7");
toothNumber.add("8");
toothNumber.add("9");
toothNumber.add("10");
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
spinnerToothNumber.setAdapter(stringArrayAdapter);
}
}
}
);
Related
Is it possible to make arithmetic calculation automatically when checkbox is pressed? I want to add the total harga (total price) change based the choice of checkbox topping prices and add them with the basic prices (product price).
ex:
checked CB Extra Ayam and CB Kepala will add 3000 + 4000
then add it with basic product price 10000
so the total price would be 17000
and the total price will back to 10000 when im unchecked CB Extra Ayam and CB Kepala and so on
Here is my code
if (Code.toUpperCase().matches((inMieAyam.toUpperCase()))){
//set text topping
CBTopping1.setText("Extra Ayam");
CBTopping2.setText("Extra Tetelan Sapi");
CBTopping3.setText("Cakar (2 pcs)");
CBTopping4.setText("Kepala");
CBTopping5.setText("Bakso Sapi");
CBTopping6.setText("Telur Mata Sapi");
CBTopping7.setText("Balungan Rica Ayam");
CBTopping8.setText("Extra Sawi");
CBTopping9.setText("Extra Acar");
//hide remaining topping CB
CBTopping10.setVisibility(View.GONE);
//set price to variable
Price1 = 3000;
Price2 = 7000;
Price3 = 4000;
Price4 = 4000;
Price5 = 3000;
Price6 = 4000;
Price7 = 4000;
Price8 = 1000;
Price9 = 1000;
Price10 = 0;
//set price topping
TxtvToppingPrice1.setText("Rp."+Price1);
TxtvToppingPrice2.setText("Rp."+Price2);
TxtvToppingPrice3.setText("Rp."+Price3);
TxtvToppingPrice4.setText("Rp."+Price4);
TxtvToppingPrice5.setText("Rp."+Price5);
TxtvToppingPrice6.setText("Rp."+Price6);
TxtvToppingPrice7.setText("Rp."+Price7);
TxtvToppingPrice8.setText("Rp."+Price8);
TxtvToppingPrice9.setText("Rp."+Price9);
//hide remaining topping price Txtv
TxtvToppingPrice10.setVisibility(View.GONE);
FinalPrice1 = 0;
FinalPrice2 = 0;
FinalPrice3 = 0;
FinalPrice4 = 0;
FinalPrice5 = 0;
FinalPrice6 = 0;
FinalPrice7 = 0;
FinalPrice8 = 0;
FinalPrice9 = 0;
FinalPrice10 = 0;
CBTopping1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
FinalPrice1 = Price1;
}
else {
FinalPrice1 = 0;
}
}
});
CBTopping2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
FinalPrice2 = Price2;
}
else {
FinalPrice2 = 0;
}
}
});
CBTopping3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
FinalPrice3 = Price3;
}
else {
FinalPrice3 = 0;
}
}
});
CBTopping4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
FinalPrice4 = Price4;
}
else {
FinalPrice4 = 0;
}
}
});
CBTopping5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
FinalPrice5 = Price5;
}
else {
FinalPrice5 = 0;
}
}
});
CBTopping6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
FinalPrice6 = Price6;
}
else {
FinalPrice6 = 0;
}
}
});
CBTopping7.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
FinalPrice7 = Price7;
}
else {
FinalPrice7 = 0;
}
}
});
CBTopping8.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
FinalPrice8 = Price8;
}
else {
FinalPrice8 = 0;
}
}
});
CBTopping9.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
FinalPrice9 = Price9;
}
else {
FinalPrice9 = 0;
}
}
});
FoodPrice1 = Integer.parseInt(Price);
TotalFinalPrice = FoodPrice1 + FinalPrice1 + FinalPrice2 + FinalPrice3 +
FinalPrice4 + FinalPrice5 + FinalPrice6 +
FinalPrice7 + FinalPrice8 + FinalPrice9 ;
TxtvTotalHarga.setText(Integer.toString(TotalFinalPrice));
}
edit :
turn out the variables is not changed in the checkbox listener
default FinalPrice1 is 0 when im changed it from checkbox listener it would not change to Price1 values, it still 0. I tried to change it directly by writing value to it FinalPrice1 = 10000 also no luck it just say 0.
if I remove the
FinalPrice1 = 0;
FinalPrice2 = 0;
FinalPrice3 = 0;
FinalPrice4 = 0;
FinalPrice5 = 0;
FinalPrice6 = 0;
FinalPrice7 = 0;
FinalPrice8 = 0;
FinalPrice9 = 0;
FinalPrice10 = 0;
it would be nullpointer error
i dont know whats wrong
i cant paste the whole code because the post didn't allow it, its too long lmao
the variable of the TotalFinalPrice just not changed or maybe not calculated
When your TextView have to be updated, use invalidate() method.
This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().
For your case use TxtvTotalHarga.invalidate();.
I think you should first handle your objects into arrays, using tag value to save initial prices, create one event listener ( handle using object'd getTag() or array index....
And important: move the setOn..... out of the if. These call should be done onStart, before you trigger this event.
And can yoi also give more code on the context that this big if is calling?
Did you debug with AS to see these events triggered?
Hello I'm a student and My project is making a application by android studio.
I'm just a beginner so everything is difficult :(
Here is my problem.
I dynamically created checkbox depending on the current.
I want to get finalMemberTag if user click the btnEx button.
But In this code, When I click the btnEx, finalMemberTag is null.
And I want that If user click the checkbox, add the checkbox's text to the finalMemberTag.
and checkbox is unchecked, remove the checkbox's text to the finalMemberTag.
How can I do these?
(I already set the drawable things.)
Please Help me...
private String[] finalMemberTag;
Button btnEx;
btnEx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendData();
}
});
Switch(current){
case "A":
String[] memberA = {"all", "ab", "cd", "ef", "g", "hu", "hi", "dd"};
for (int i = 0; i < 8; i++) {
CheckBox chA = new CheckBox(getApplicationContext());
chA.setText(membersA[i]);
chA.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
chA.setTextColor(Color.parseColor("#8D8D8D"));
cHA.setButtonDrawable(android.R.color.transparent);
chA.setBackground(inactiveCb);
int index = i;
chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
chA.setBackground(activeCb);
chA.setTextColor(startextColor);
finalMemberTag[index] = buttonView.getText().toString();
}
if(!isChecked) {
chA.setBackground(inactiveCb);
chA.setTextColor(Color.parseColor("#8D8D8D")); }
}
});
chA.setPadding(36, 24, 36, 24);
chA.setLayoutParams(params);
placeInputMember.addView(chA);
}
break;
case "B":
String[] memberB = {"all", "abc", "Dcd", "e3f", "DSg", "DGu", "hE", "dV"};
for (int i = 0; i < 8; i++) {
CheckBox chB = new CheckBox(getApplicationContext());
chB.setText(memberB[i]);
chB.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
chB.setTextColor(Color.parseColor("#8D8D8D"));
chB.setButtonDrawable(android.R.color.transparent);
chB.setBackground(inactiveCb);
int index = i;
chB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
chB.setBackground(activeCb);
chB.setTextColor(startextColor);
finalMemberTag[index] = buttonView.getText().toString();
}
if(!isChecked) {
chB.setBackground(inactiveCb);
chB.setTextColor(Color.parseColor("#8D8D8D")); }
}
});
chB.setPadding(36, 24, 36, 24);
chB.setLayoutParams(params);
placeInputMember.addView(chB);
}
break;
}
public void sendData(){
Log.d("##", String.valueOf(finalMemberTag));
}
ANOTHER ERROR
String[] membersA = {"all", "ji", "ha", "bo", "re", "co", "zo", "pi"};
arrayMember = new ArrayList<>();
for (int i = 0; i < membersBts.length; i++) {
CheckBox chA = new CheckBox(getApplicationContext());
chA .setText(members[i]);
chA.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
chA.setTextColor(Color.parseColor("#8D8D8D"));
chA .setButtonDrawable(android.R.color.transparent);
chA.setBackground(inactiveCb);
int index = i;
chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
chA.setBackground(activeCb);
chA.setTextColor(startextColor);
arrayMember.add(index, buttonView.getText().toString());
}
if(!isChecked) {
chA.setBackground(inactiveCb);
chA.setTextColor(Color.parseColor("#8D8D8D"));
arrayMember.remove(index);
}
}
});
chA.setPadding(36, 24, 36, 24);
chA.setLayoutParams(params);
placeInputMember.addView(chA);
}
break;
ERROR LOGCAT
java.lang.IndexOutOfBoundsException: Index: 5, Size: 2
Ok, you have some issues with your code:
First, finalMemberTag is always null because you never initialized it. If you want to use a string array and you want to store the text on the checkboxes then we should assume that at most you'll have all of the checkboxes (16) selected. This means you'll want to initialize your array as:
private String[] finalMemberTag = new String[16]
Second, I think you'd be better off using a string set to avoid duplicates, or, if you need the information on a particular index, an arraylist.
With the Set:
private Set<String> finalMembersTag = new HashSet<>(16);
//onCreate...other stuff...for loop
//...setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final String buttonText = buttonView.getText().toString();
if (isChecked) {
// other stuff ...
finalMembersTag.add(buttonText);
} else {
// other stuff ...
finalMembersTag.remove(buttonText);
}
}
With the ArrayList:
private List<String> finalMembersTag = new ArrayList<>(16);
//onCreate...other stuff...for loop
int index = i;
chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final String buttonText = buttonView.getText().toString();
if (isChecked) {
// other stuff ...
finalMembersTag.add(buttonText); //this will add the text
} else {
// other stuff ...
finalMembersTag.remove(buttonText); //this will remove the element if found, or if you want an empty string you can do finalMembersTag.add(index, "");
}
}
i am facing an issue but it is not showed in LOGCAT as it is not a code error or null
the issue is that if the question has 2 answers and user selects both of them it counts in right answer but if the question has only 1 answer the game calculates it to wrong answer even if the user selected the right answer i will post a screenshot for that enter image description here
i have already coded that the correct answer would be bold and red as you can see
this is my Question fragment code
public class QuestionFragment extends Fragment implements IQuestion {
TextView txt_question_text;
CheckBox ckbA, ckbB, ckbC, ckbD;
FrameLayout layout_image;
ProgressBar progressBar;
Question question;
int questionIndex = -1;
public QuestionFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View itemView = inflater.inflate(R.layout.fragment_question, container, false);
//GET QUESTION
questionIndex = getArguments().getInt("index", -1);
question = Common.questionList.get(questionIndex);
if (question != null) {
layout_image = (FrameLayout) itemView.findViewById(R.id.layout_image);
progressBar = (ProgressBar) itemView.findViewById(R.id.progress_bar);
if (question.isImageQuestion()) {
ImageView img_question = (ImageView) itemView.findViewById(R.id.img_question);
Picasso.get().load(question.getQuestionImage()).into(img_question, new Callback() {
#Override
public void onSuccess() {
progressBar.setVisibility(View.GONE);
}
#Override
public void onError(Exception e) {
Toast.makeText(getContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} else
layout_image.setVisibility(View.GONE);
//VIEW
txt_question_text = (TextView) itemView.findViewById(R.id.txt_question_text);
txt_question_text.setText(question.getQuestionText());
ckbA = (CheckBox) itemView.findViewById(R.id.ckbA);
ckbA.setText(question.getAnswerA());
ckbA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
Common.selected_values.add(ckbA.getText().toString());
else
Common.selected_values.remove(ckbA.getText().toString());
}
});
ckbB = (CheckBox) itemView.findViewById(R.id.ckbB);
ckbB.setText(question.getAnswerB());
ckbB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
Common.selected_values.add(ckbB.getText().toString());
else
Common.selected_values.remove(ckbB.getText().toString());
}
});
ckbC = (CheckBox) itemView.findViewById(R.id.ckbC);
ckbC.setText(question.getAnswerC());
ckbC.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
Common.selected_values.add(ckbC.getText().toString());
else
Common.selected_values.remove(ckbC.getText().toString());
}
});
ckbD = (CheckBox) itemView.findViewById(R.id.ckbD);
ckbD.setText(question.getAnswerD());
ckbD.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
Common.selected_values.add(ckbD.getText().toString());
else
Common.selected_values.remove(ckbD.getText().toString());
}
});
}
return itemView;
}
#Override
public CurrentQuestion getSelectedAnswer() {
//THIS METHOD WILL RTURN STATE OF ANSWER
//RIGHT , WRONG OR NO ANSWER
CurrentQuestion currentQuestion = new CurrentQuestion(questionIndex, Common.ANSWER_TYPE.NO_ANSWER);//DEFAULT IS NO ANSWER
StringBuilder result = new StringBuilder();
if (Common.selected_values.size() > 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
for (int i = 0; i < arrayAnswer.length; i++)
if (i < arrayAnswer.length - 1)
result.append(new StringBuilder(((String) arrayAnswer[i]).substring(0, 1)).append(","));
else
result.append(new StringBuilder((String) arrayAnswer[i]).substring(0, 1));
} else if (Common.selected_values.size() == 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
result.append((String) arrayAnswer[0]).substring(0, 1);
}
if (question != null) {
//COMPARE CORRECT ANSWER FROM DATABASE WITH USER SELECTED ANSWER
if (!TextUtils.isEmpty(result)) {
if (result.toString().equals(question.getCorrectAnswer()))
currentQuestion.setType(Common.ANSWER_TYPE.RIGHT_ANSWER);
else
currentQuestion.setType(Common.ANSWER_TYPE.WRONG_ANSWER);
} else
currentQuestion.setType(Common.ANSWER_TYPE.NO_ANSWER);
}
else {
Toast.makeText(getContext(), "cannot get question", Toast.LENGTH_SHORT).show();
currentQuestion.setType(Common.ANSWER_TYPE.NO_ANSWER);
}
Common.selected_values.clear();//ALWAYS CLEAR SELECTED ANSWERS AFTER COMPARE IS DONE
return currentQuestion;
}
#Override
public void showCorrectAnswer() {
//BOLD CORRECT ANSWER
//PARTERN:A,B
String[] correctAnswer = question.getCorrectAnswer().split(",");
for (String answer : correctAnswer) {
if (answer.equals("A")) {
ckbA.setTypeface(null, Typeface.BOLD);
ckbA.setTextColor(Color.RED);
} else if (answer.equals("B")) {
ckbB.setTypeface(null, Typeface.BOLD);
ckbB.setTextColor(Color.RED);
} else if (answer.equals("C")) {
ckbC.setTypeface(null, Typeface.BOLD);
ckbC.setTextColor(Color.RED);
} else if (answer.equals("D")) {
ckbD.setTypeface(null, Typeface.BOLD);
ckbD.setTextColor(Color.RED);
}
}
}
#Override
public void disapleAnswer() {
ckbA.setEnabled(false);
ckbB.setEnabled(false);
ckbC.setEnabled(false);
ckbD.setEnabled(false);
}
#Override
public void resetQuestion() {
//ENABLE CHECKBOX
ckbA.setEnabled(true);
ckbB.setEnabled(true);
ckbC.setEnabled(true);
ckbD.setEnabled(true);
//REMOVE ALL SELECTED
ckbA.setChecked(false);
ckbB.setChecked(false);
ckbC.setChecked(false);
ckbD.setChecked(false);
//REMOVE ALL BOLD ON TEXT
ckbA.setTypeface(null, Typeface.NORMAL);
ckbA.setTextColor(Color.BLACK);
ckbB.setTypeface(null, Typeface.NORMAL);
ckbB.setTextColor(Color.BLACK);
ckbC.setTypeface(null, Typeface.NORMAL);
ckbC.setTextColor(Color.BLACK);
ckbD.setTypeface(null, Typeface.NORMAL);
ckbD.setTextColor(Color.BLACK);
}
}
i think the error will be in this part of Question fragment in logic but can not get it (it is for if the correct answer has 2 values or 1 value)
#Override
public CurrentQuestion getSelectedAnswer() {
//THIS METHOD WILL RTURN STATE OF ANSWER
//RIGHT , WRONG OR NO ANSWER
CurrentQuestion currentQuestion = new CurrentQuestion(questionIndex, Common.ANSWER_TYPE.NO_ANSWER);//DEFAULT IS NO ANSWER
StringBuilder result = new StringBuilder();
if (Common.selected_values.size() > 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
for (int i = 0; i < arrayAnswer.length; i++)
if (i < arrayAnswer.length - 1)
result.append(new StringBuilder(((String) arrayAnswer[i]).substring(0, 1)).append(","));
else
result.append(new StringBuilder((String) arrayAnswer[i]).substring(0, 1));
} else if (Common.selected_values.size() == 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
result.append((String) arrayAnswer[0]).substring(0, 1);
}
just found out the answer but i will not delete the post so if someone else got same issue
i simply removed the second part and edited the first part condition as follow
CurrentQuestion currentQuestion = new CurrentQuestion(questionIndex, Common.ANSWER_TYPE.NO_ANSWER);//DEFAULT IS NO ANSWER
StringBuilder result = new StringBuilder();
if (Common.selected_values.size() >= 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
for (int i = 0; i < arrayAnswer.length; i++)
if (i < arrayAnswer.length - 1)
result.append(new StringBuilder(((String) arrayAnswer[i]).substring(0, 1)).append(","));
else
result.append(new StringBuilder((String) arrayAnswer[i]).substring(0, 1));
} /*else if (Common.selected_values.size() == 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
result.append((String) arrayAnswer[0]).substring(0, 1);
}*/
hey how can i know that the below checkbox when clicked is from which row of the list.
is there any way of knowing that. Every time yo is returning false which is its default value.
checkBox.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
callBlockOptions callBlockOptions = (callBlockOptions) cb.getTag();
callBlockOptions.setChecked( cb.isChecked() );
String yo;
if(callBlockOptions.getPosition()=="0")
{
callBlockOptions.setAllCalls();
}
if(callBlockOptions.getAllCalls() ==true)
yo="true";
else
yo="false";
Toast.makeText(getContext(), callBlockOptions.getPosition(), Toast.LENGTH_LONG).show();
}
});
}
After seeing your code, one thing you might want to try is setting listeners for the individual children within the ListView. You could do something like this:
ListView listView = (ListView)findViewById(R.id.listView);
int count = listView.getChildCount();
for (int x = 0; x < count; x++)
{
Class<? extends View> c = listView.getChildAt(x).getClass();
if (c == CheckBox.class)
{
CheckBox checkBox = (CheckBox)(listView.getChildAt(x));
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton compoutButton, boolean isChecked) {
// TODO Auto-generated method stub
callBlockOptions.isChecked = isChecked;
}
});
}
else if (c == TextView.class)
{
TextView textView = (TextView)(listView.getChildAt(x));
textView.setOnEditorActionListener(new OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView tView, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
callBlockOptions.name = tView.getText().toString();
return true;
}
});
}
You probably want your other class to be like this:
public class callBlockOptions {
public static String name;
public static Boolean isChecked;
}
Then you can get and set name and isChecked like this:
callBlockOptions.isChecked = false;
Boolean boolVal = callBlockOptions.isChecked;
I am having an array of String which is being used to show in a spinner.
Now my need is
When the user giving their input in the edit text have to sort the array based on the edit text values ans have to show it in the spinner. I cannot get the logic fully.
I have tried like this.,
String[] a1 = { "Android", "Apple", "Andrew", "Ball" }, a2 = null;
EditText mn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mn = (EditText) findViewById(R.id.plus);
a2 = a1;
mn.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
Arrays.sort(a1);
doCall();
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
});
}
protected void doCall() {
for (int i = 0; i < a1.length; i++) {
String s = a1[i];
for (int j = 0; j < mn.getText().toString().length(); j++) {
if (Character.toLowerCase(s.charAt(j)) == Character
.toLowerCase(mn.getText().toString().charAt(j))) {
if (j == mn.getText().toString().length() - 1) {
String temp = a1[0];
a1[0] = a2[i];
a1[i] = temp;
} else {
continue;
}
} else {
break;
}
}
}
}
IF i am giving "a" means i am getting "Andrew,Ball,Apple,Android"
but What i need to be shown is "Andrew,Android,Apple,Ball" How can i achieve this. thanks in advance,
You could try call Arrays.sort(names) before end of doCall() and then notify yours Spinner Adapter.
## AutoComplete ##
Try using the autoComplete functionality:
public class CountriesActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Android", "Apple", "Andrew", "Ball"
};
}