when listview scroll that time edittext set defaul

2019-06-27 05:11发布

问题:

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.