How to show RadioButton and EditText inside ListView?

I want to implement RadioButton and EditText inside a ListView row. I am using an ArrayAdapter to populate a list. But the problem is that when I select RadioButton and scroll down the list and scroll again, the Radiobutton that was selected is not selected. Same thing with EditText content. Text is deleted when scrolling up.

+3
source share
3 answers

Check the adapter, you may not be doing the work on bindView(). You must set to bindView()values again .

, , , . newView() 5-10 (, ), . ListView 200 , 5-10 , , bindView(). / .

+2

, , , . , 1000 .

- , , , edittext, , , .

, getView.

+2

GetView function in the base adapter. Saves the value of edittext 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 ;
            }
0
source

Source: https://habr.com/ru/post/1750483/


All Articles