Android自定义ArrayAdapter多次调用getView方法 - 重置动态TextView值(Android custom ArrayAdapter getView method called multiple times - resetting dynamic TextView value)

我的自定义ArrayAdapter中的getView方法被多次调用,我认为这是有意的。 问题是,我有一个动态设置的数量TextView但是当你滚动并且框离开屏幕时,该值消失了。 我不确定我做错了什么,谷歌并没有证明它有多大帮助。 希望有人在这里可以帮助我。

适应器称为:

adapter = new MenuAdapter(thisActivity, R.layout.menu, list); setListAdapter(adapter);

我的自定义ArrayAdapter:

public MenuAdapter(Context context, int textViewResourceId, ArrayList<Object> menu) { super(context, textViewResourceId, menu); this.menu = menu; vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; Object cat = menu.get(position); if (cat.getClass().equals(Category.class)) { v = vi.inflate(R.layout.category, null); Category item = (Category)cat; v.setOnClickListener(null); v.setOnLongClickListener(null); v.setLongClickable(false); TextView tt = (TextView) v.findViewById(R.id.category); tt.setText(item.getName()); } else if (cat.getClass().equals(OrderItem.class)) { v = vi.inflate(R.layout.menu, null); OrderItem orderItem = (OrderItem)cat; Item item = orderItem.getItem(); TextView tt = (TextView) v.findViewById(R.id.title); tt.setText(item.getName()); TextView bt = (TextView) v.findViewById(R.id.desc); bt.setText(item.getDescription()); TextView qty = (TextView) v.findViewById(R.id.qty); qty.setId(item.getId()); ImageButton minus = (ImageButton) v.findViewById(R.id.qtyMinus); minus.setTag(item); ImageButton plus = (ImageButton) v.findViewById(R.id.qtyPlus); plus.setTag(item); } return v; }

菜单布局:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:padding="6dip" android:background="#FFFFFF"> <RelativeLayout android:orientation="vertical" android:layout_width="0dip" android:layout_weight="1" android:layout_height="fill_parent" android:cacheColorHint="#FFFFFF" android:background="#FFFFFF"> <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_vertical" android:textSize="16px" android:textColor="#000000" /> <TextView android:id="@+id/desc" android:layout_below="@id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="11px" android:textStyle="italic" android:textColor="#000000" /> </RelativeLayout> <ImageButton android:id="@+id/qtyMinus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/minus" android:paddingTop="15px" android:onClick="minusQty" /> <TextView android:id="@+id/qty" android:layout_width="50px" android:layout_height="50px" android:textColor="#000000" android:textSize="18px" android:gravity="center_vertical|center_horizontal" android:freezesText="true" android:background="@android:drawable/editbox_background"/> <ImageButton android:id="@+id/qtyPlus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/plus" android:paddingTop="15px" android:onClick="addQty" /> </LinearLayout>

任何帮助将不胜感激。

非常感谢。

The getView method in my custom ArrayAdapter is getting called multiple times, which I assume it is meant to. Problem is, I have a quantity TextView which is dynamically set but when you scroll and the box goes off screen the value disappears. I am not sure what I am doing wrong and Google is not proving to be much help. Hopefully someone here can help me.

The adapater is called:

adapter = new MenuAdapter(thisActivity, R.layout.menu, list); setListAdapter(adapter);

My Custom ArrayAdapter:

public MenuAdapter(Context context, int textViewResourceId, ArrayList<Object> menu) { super(context, textViewResourceId, menu); this.menu = menu; vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; Object cat = menu.get(position); if (cat.getClass().equals(Category.class)) { v = vi.inflate(R.layout.category, null); Category item = (Category)cat; v.setOnClickListener(null); v.setOnLongClickListener(null); v.setLongClickable(false); TextView tt = (TextView) v.findViewById(R.id.category); tt.setText(item.getName()); } else if (cat.getClass().equals(OrderItem.class)) { v = vi.inflate(R.layout.menu, null); OrderItem orderItem = (OrderItem)cat; Item item = orderItem.getItem(); TextView tt = (TextView) v.findViewById(R.id.title); tt.setText(item.getName()); TextView bt = (TextView) v.findViewById(R.id.desc); bt.setText(item.getDescription()); TextView qty = (TextView) v.findViewById(R.id.qty); qty.setId(item.getId()); ImageButton minus = (ImageButton) v.findViewById(R.id.qtyMinus); minus.setTag(item); ImageButton plus = (ImageButton) v.findViewById(R.id.qtyPlus); plus.setTag(item); } return v; }

The menu layout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:padding="6dip" android:background="#FFFFFF"> <RelativeLayout android:orientation="vertical" android:layout_width="0dip" android:layout_weight="1" android:layout_height="fill_parent" android:cacheColorHint="#FFFFFF" android:background="#FFFFFF"> <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_vertical" android:textSize="16px" android:textColor="#000000" /> <TextView android:id="@+id/desc" android:layout_below="@id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="11px" android:textStyle="italic" android:textColor="#000000" /> </RelativeLayout> <ImageButton android:id="@+id/qtyMinus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/minus" android:paddingTop="15px" android:onClick="minusQty" /> <TextView android:id="@+id/qty" android:layout_width="50px" android:layout_height="50px" android:textColor="#000000" android:textSize="18px" android:gravity="center_vertical|center_horizontal" android:freezesText="true" android:background="@android:drawable/editbox_background"/> <ImageButton android:id="@+id/qtyPlus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/plus" android:paddingTop="15px" android:onClick="addQty" /> </LinearLayout>

Any help will be greatly appreciated.

Many Thanks.

最满意答案

当你注意到时,会多次调用getView() 。 它应该没关系,因为你的数组适配器是基于它的内部数据模型的状态(数组,对象列表,等等)。 getView()应该是幂等的,因为多次调用它不应该改变结果。

你说“当你滚动并且盒子离开屏幕时,值会消失”。 注意肯定是什么意思。 当您将getView()生成的其中一个视图从可见区域滚动时,当您向后滚动时,值是不同的? 没有任何其他信息,我不得不说这是不可能的。 原因是,除非您正在修改适配器的内部状态或更改适配器,否则您将始终为给定位置生成相同的视图。

顺便说一下, convertView可以为null,所以你想要做的事情,

View v = convertView; if (v == null) { v = inflater.inflate(...); } // now use v in the rest of the method

getView() will be called multiple times as you note. it shouldn't matter, because your array adapter is based on the state of it's internal data model (array, list of objects, whatever). getView() should be idempotent in that calling it multiple times shouldn't change the result.

you say "when you scroll and the box goes off screen the value disappears". note sure what mean. when you scroll one of the views generated in getView() off the visible area, and when you scroll it back, the value is different? without any other information, i'd have to say that's not possible. the reason is again that unless you are modifying the internal state of the adapter, or changing the adapter, you will always generate the same view for a given position.

by the way, convertView can be null, so you want to do something like,

View v = convertView; if (v == null) { v = inflater.inflate(...); } // now use v in the rest of the method

更多推荐