自定义ListView滚动时会随机更改按钮文本或edittext [英] Customized ListView when scrolling changes randomly button text or edittext

查看:92
本文介绍了自定义ListView滚动时会随机更改按钮文本或edittext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索过这个,但无济于事,我还没有确切的答案.

I have searched for this one but to no avail I haven't get the exact answer to the issue.

我在ListView内有一个按钮,单击该按钮时会显示一个警告对话框,用于选择日期,选择日期后,所选日期将是按钮的文本.该代码工作正常,但当我上下滚动时,日期将被随机更改 这是我的适配器getView()方法的代码.当我键入任何EditText的效果都很好,但是当我开始滚动值时,EditText也会发生这种情况.

I have a button inside a ListView, when that button is clicked it shows an alert dialog to choose a date and after choosing date, the selected date will be the text of the button. The code works fine but when i scroll down and up the date will be randomly change here is my code for the getView() method of my adapter.This also happens to the EditText when i type to any of the EditText works fine but when i start scrolling the values also change.

这是我的get视图方法

Here is my get view method

public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;
    if(convertView == null){
        convertView = layoutInflater.inflate(R.layout.repeat_entry_listview,null);

        viewHolder = new ViewHolder();
        viewHolder.btnDate = (Button) convertView.findViewById(R.id.rpbtnDate);
        viewHolder.txtNotes  = (EditText) convertView.findViewById(R.id.rpNotes);
        convertView.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }


    RepeatEntryList repeatEntryList = listData.get(position);
    viewHolder.btnDate.setText(repeatEntryList.getDate());
    viewHolder.btnDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          //EVEN IF I REMOVE THE CODE FOR SETTING THE BUTTON TEXT TO JUST LIKE THE BELOW CODE. SCROLLING ISSUES STILL PERSIST

          viewHolder.btnDate.setText("just sample date text");

            //**NOTE UPDATED**
            //MY EDITTEXT ALSO HAVE THE SAME BEHAVIOR
        }
    });
    viewHolder.txtNotes.setHint(contx.getResources().getString(R.string.placeholder_txt));
    return convertView;
}

注意,假设我在ListView中有20个按钮和20个EditText.当我在所有EditText上键入内容并单击更改按钮的文本时,然后上下滚动ListViewEditText和按钮的文本或值将更改或互换,有时消失.

Note Let's say i have 20 buttons and 20 EditText inside the ListView. When i type something to all of the EditText and change the text of the button by clicking it ,then scroll down and up the ListView ,the text or values of EditText and button will change or interchange and sometimes gone.

关于我的问题,有人可以向我解释为什么ListView如此行为以及如何避免这种特定行为.

With regards to my problem can someone explain to me why ListView is behaving like this and how to avoid this certain behavior.

推荐答案

如果有人可能遇到相同的问题,这就是我为解决我的问题而做的事情:
1.首先,我在我的自定义Adapter类(即RepeatEntryListAdapter)中创建了一个变量,并将其命名为listData

This is what i have done to solve my problem is incase someone might have the same problem:
1. First i create i variable inside my custom Adapter class which is RepeatEntryListAdapter and called it listData

public ArrayList<RepeatEntryList> listData;

2.我引用listData变量作为RepeatEntryList类的数组列表,这是我的getter和setter类,

2. i reference listData variable to be the array list of RepeatEntryList class which is my getter and setter class like this.

public RepeatEntryListAdapter(Context context,ArrayList<RepeatEntryList> listdata){
          this.listData = listdata;
            this.contx = context;
            mainActivity = (MainActivity) context;
            layoutInflater= LayoutInflater.from(context);
       }

3.在getview方法中,我为按钮创建了click事件.在click事件中,我实例化了getter类,然后使用我的getter类的实例化变量并使用settext来设置按钮的文本,还设置了的日期lisData 变量.重要的部分是使用 listData 变量,其中我使用position变量来设置 listData 变量的某个位置的值,例如这个.

3. Inside getview method i created click event for the button .Inside the click event I instantiated my getter class then use the instantiated variable of my getter class and use settext,for setting the text of the button and also set the date for the lisData variable.and the important part is use the listData variable in which i use the position variable to set the value of a certain position of the listData varaible like this.

 RepeatEntryList re = rListData;
 viewHolder.btnDate.setText('07-06-2015')//say the date selected is the string inside setText                          re.setDate(viewHolder.btnDate.getText().toString());//set Date is a setter method inside my RepeatEntryList class
 listData.set(position, re);//position variable should be final

4.这是最棘手的部分,它是解决我的自定义列表视图的滚动问题的一部分,该问题是由滚动时触发的edittext watcher引起的.我所做的是.

4. This is the most trickiest part which is part in solving the scrolling problem of my custom list view which is caused by the edittext watcher which fires when scrolling . what i did is.

a.我创建了一个实现这样的TextWatcher的类

a. i Created a class that implements TextWatcher like this

class EditTextWatcher implements TextWatcher{

        private int mPosition;
        private boolean mActive;

        void setPosition(int position){
            mPosition = position;
        }
        void setActive(boolean active){
            mActive = active;
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(mActive){
                //rNotes.set(mPosition,s.toString());
                RepeatEntryList re = listData.get(mPosition);
                re.setNotes(s.toString());
                listData.set(mPosition,re);
            }
        }
    }

b.我还在静态类ViewHolder中为EditeTextWatcher创建了一个变量

b. i also created a variable for EditeTextWatcher inside my static class ViewHolder

 static class ViewHolder{
   public Button btnDate;
   public EditText txtNotes;
   public EditTextWatcher mWatcher;
}

c.在我的getview方法中,我创建了一个EditTextWatcher()的新实例,该实例也创建了一个新的textwatcher,并且不使用last事件,因为我创建了textwatcher的新实例

c. inside my getview method ,i created a new instance of EditTextWatcher() which also create a new textwatcher and not use the last event because i create a new instance of the textwatcher

public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder viewHolder;

        final RepeatEntryList rListData = listData.get(position);

        if(convertView == null){
            convertView = layoutInflater.inflate(R.layout.repeat_entry_listview,null);

            viewHolder = new ViewHolder();
            viewHolder.btnDate = (Button) convertView.findViewById(R.id.rpbtnDate);
            viewHolder.mWatcher = new EditTextWatcher();
            viewHolder.txtNotes  = (EditText) convertView.findViewById(R.id.rpNotes);
            viewHolder.txtNotes.addTextChangedListener(viewHolder.mWatcher);

            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();


        }

        viewHolder.btnDate.setText(rListData.getDate());

        viewHolder.mWatcher.setActive(false);
        viewHolder.txtNotes.setText(rListData.getNotes());
        viewHolder.mWatcher.setPosition(position);
        viewHolder.mWatcher.setActive(true);


      //here is the part of button click event i mention above (no 3.)
return convertView;
}

这篇关于自定义ListView滚动时会随机更改按钮文本或edittext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆