Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

How to set increment counter in TextWatcher in getView method ?

$
0
0

I'm trying to add some results from a quiz so that when user gets each right answer it adds 1 to it but the code below never gives me the right answer the 'correctAns = correctAns+1' gives me all 5 at once, if I make the correctAns as local variable this will give me 1 for every right answer but it will not add up. Can any one please help.

```public class AdapterListView extends ArrayAdapter {

        private int correctAns = 0;

         public AdapterListView(Context context, int resource, ArrayList<Questions> questionsArrayList) {
            super(context, resource);
            this.mContext = context;
            this.setQuestionsArrayList(questionsArrayList);
            this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        ....
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ...

            holder.editText.addTextChangedListener(new TextWatcher() {

                public void afterTextChanged(Editable s) {

                    if (holder.editText.getText().toString().trim().length() > 0) {
                        int inputNum = Integer.parseInt(String.valueOf(holder.editText.getText().toString().trim()));

                        if (getAnswersList().get(position) != inputNum) {
                            holder.imgFalse.setVisibility(View.VISIBLE);
                            holder.imgTrue.setVisibility(View.GONE);
                        } else {

                            correctAns = correctAns+1;

                            holder.imgTrue.setVisibility(View.VISIBLE);
                            holder.imgFalse.setVisibility(View.GONE);
                        }                    
                    }
                }

                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }
            });

            return convertView;
        }

        static class ViewHolder {
            TextView questionTextView;
            EditText editText;
            ImageView imgTrue, imgFalse;
        }
    }`

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>