0
我有一個BaseAdapter如下:getView位置不檢索正確
public class OrderAdapter extends BaseAdapter {
private final Context mContext;
private Handler mHandler;
private List<Order> mOrders = Collections.emptyList();
private Message msg;
private int counter;
public OrderAdapter(Context context, List<Order> objects) {
ThreadPreconditions.checkOnMainThread();
this.mContext = context;
this.mOrders = objects;
notifyDataSetChanged();
}
public OrderAdapter(Context context, List<Order> objects, Handler handler) {
ThreadPreconditions.checkOnMainThread();
this.mContext = context;
this.mOrders = objects;
this.mHandler = handler;
notifyDataSetChanged();
}
public void updateOrders(List<Order> bananaPhones) {
ThreadPreconditions.checkOnMainThread();
this.mOrders = bananaPhones;
notifyDataSetChanged();
}
@Override
public int getCount() {
return mOrders.size();
}
@Override
public Order getItem(int position) {
return mOrders.get(position);
}
// getItemId() is often useless, I think this should be the default
// implementation in BaseAdapter
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
msg = Message.obtain();
if (convertView == null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.order_row_item, parent, false);
}
msg.what = position;
msg.obj = convertView;
mHandler.sendMessage(msg);
return convertView;
}
}
,並在我的活動我有一個接收和asynchronouslly改變行的值的處理程序。問題是:getView檢索的「位置」不正確,它實際上是從0到4的隨機數。在調試會話中,我發現適配器有正確的項目,只是讓視圖出錯。我的listView不在scrollView中,它的高度是fill_parent,並且適配器上沒有asyncTask,所以即使是創建新的thead也是由activity完成的(我想這是重置數字,但情況並非如此) 。
現在已經堅持了2天,任何幫助將不勝感激。在getView
當你說'「位置」由getView'檢索是不正確的,你的意思是「位置傳遞到getView」,或消息中發送給處理程序的位置? – cybersam
你想用Handler達到什麼目的?動態調用getView()以生成ListView中的行,生成行的順序無關緊要。 – BladeCoder
getView將被調用的時間和順序的數量不能保證。所以它是一個預期的行爲 –