Question

I want to implement RadioButton and EditText inside a row of a ListView. I am using an ArrayAdapter to populate the List. But the problem is that when i am selecting a RadioButton and scroll down the list and scroll up again the Radiobutton which had been selected is unselected. Same with the content of the EditText. The text getting removed when I scroll up.

Was it helpful?

Solution

Check your adapter, you are probably don't do the job right on bindView(). You have to set again on bindView() the values.

I will reformulate the sentence and you will probably will understand. The newView() creates only 5-10 views (as many they fit on the screen), and they are reused for other rows. If you have a ListView with 200 lines in it, actually you have only 5-10 views, and you have to make sure you update the views with the valid changes in bindView(). You have to store/save the changes to an object for later reuse.

OTHER TIPS

The problem is that all views that leave the screen may get destroyed to save memory on the phone. If the phone would not do this a list with 1000 entries would fill the memory of your device.

Because of that the state change that is happening to the radiobutton and the text that was inserted in the edittext will simply be deleted and if the user scrolls up the view will be recreated.

You need to save the state of the radiobutton and the edit text in your adapter and reapply the state at the moment you recreate the view for that special item in you getView method.

getView function in Base Adapter. Saves the edittext value and displays the same if convertView is not null.

    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_row_person, null);
            holder = new ViewHolder();
            holder.PersonNameView = (TextView) convertView.findViewById(R.id.PersonNameView);
            holder.SpendAmount = (EditText) convertView.findViewById(R.id.SpendAmt);
            holder.SpendAmount.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub
                    int position2 = holder.SpendAmount.getId();
                    EditText Caption = (EditText) holder.SpendAmount;
                    Person per= (Person)holder.SpendAmount.getTag();
                    //SpendAmount is of Double type
                    if(s.toString().length()>0){
                    per.setSpendAmount(Double.parseDouble(s.toString()));
                    per.setFlag(true);}
                    else{
                         Toast.makeText(getApplicationContext(), "Please enter some value", Toast.LENGTH_SHORT).show();
                        }





                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub

                }
            });




            holder.SpendAmount.setTag(listData.get(position));
            convertView.setTag(holder);

        } else {

            ((ViewHolder)convertView.getTag()).SpendAmount.setTag(listData.get(position));
            holder = (ViewHolder) convertView.getTag();

        }


        holder.PersonNameView.setText(listData.get(position).getPersonName());
        holder.SpendAmount.setText(listData.get(position).getSpendAmount().toString());



        return convertView;




    }
     class ViewHolder {
        TextView PersonNameView;
        EditText SpendAmount ;
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top