我有带有 edittext 的列表视图和-/+按钮。当我单击按钮时更改编辑文本值,例如递增/递减.
当我设置值时5并滚动列表视图,将 edittext 设置值作为默认值如 0。
我正在使用这种方式
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;
}
您的情况的问题是,每次打开列表视图时都会调用 getView (这很好),而且当您向上/向下滚动时也会调用 getView 。为了减少内存使用,listview 会销毁所有在 listview 可见部分之外的元素,并在向后滚动时重新创建它们。
因此,为了避免这种情况,您可以使用:
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;
有关如何在列表视图中创建编辑文本的详细教程,请参阅:http://vikaskanani.wordpress.com/2011/07/27/android-focusable-edittext-inside-listview/ http://vikaskanani.wordpress.com/2011/07/27/android-focusable-edittext-inside-listview/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)