ErvalhouS
ErvalhouS

Reputation: 4216

getView position is not retrieving the correctly

I have a BaseAdapter as follows:

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;
}


}

and on my activity I have a handler that receives and changes values of rows asynchronouslly. The promblem is: the "position" retrieved by getView is not correct, it actually goes randomly from 0-4. On a debugging session I discovered that the adapter has the correct items, it's just getting the views wrong. My listView is not on a scrollView, it's height is fill_parent and there is no asyncTask on the adapter, so even the creation of new thead is done by the activity(I tought that this was resetting the numbers, but that is not the case).

Been stuck on this for 2 days now, any help would be much appreciated.

Upvotes: 0

Views: 167

Answers (1)

Related Questions