when listview scroll that time edittext set default value

回眸只為那壹抹淺笑 提交于 2019-12-23 13:07:33

问题


I have listview with edittext and -/+ button.When i am click on button change edittext value like increment/decrement.

when i am set value like 5 and scroll the listview the edittext set value as a default value like 0.

I am useing this way

public View getView(final int position, View convertView,ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View connection;
        final ViewHolder holder;
        connection = new View(context);         
        connection = inflater.inflate(R.layout.list_row_modified, null);
        holder = new ViewHolder();
        holder.up = (Button) connection.findViewById(R.id.btn_plus);
        holder.down = (Button) connection.findViewById(R.id.btn_minus);
        holder.date = (TextView) connection.findViewById(R.id.text_date);

        //onClick with holder.up/holder.down

        connection.setTag(holder);
        return connection;
    }

回答1:


The problem in your case is that your getView is called each tiem you open your listview (which is fine), but also when you scroll up/down. In order to reduce memory usage, listview will destroy all the elements which are outside of the visible part of the listview, and create them again when you scroll back.

So, in order to avoid that, you can use:

if (convertView == null) {
    holder = new ViewHolder();
    holder.up = (Button) connection.findViewById(R.id.btn_plus);
    holder.down = (Button) connection.findViewById(R.id.btn_minus);
    holder.date = (TextView) connection.findViewById(R.id.text_date);  
}

convertView.setTag(holder);
return convertView;

For detailed tutorial on how to create a edittext inside a listview, see: http://vikaskanani.wordpress.com/2011/07/27/android-focusable-edittext-inside-listview/




回答2:


you have used the ViewHolder pattern incorrectly. since you have a small list with EditText, you don't even need it.

and as Milos said, it will recycle your list items so your values will set to 0 when getView() called. you have to keep some reference to the values and set them in getView() method.



来源:https://stackoverflow.com/questions/14479178/when-listview-scroll-that-time-edittext-set-default-value

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