How to show a RadioButton and an EditText inside ListView?

偶尔善良 提交于 2019-12-10 10:07:27

问题


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.


回答1:


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.




回答2:


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.




回答3:


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 ;
            }


来源:https://stackoverflow.com/questions/3061399/how-to-show-a-radiobutton-and-an-edittext-inside-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!