【问题标题】:getView position is not retrieving the correctlygetView 位置未正确检索
【发布时间】:2015-12-13 12:39:28
【问题描述】:

我有一个 BaseAdapter 如下:

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


}

在我的活动中,我有一个处理程序,可以异步接收和更改行的值。问题是:getView 检索到的“位置”不正确,实际上是从 0-4 随机变化的。在调试会话中,我发现适配器具有正确的项目,只是视图错误。我的 listView 不在 scrollView 上,它的高度是 fill_parent 并且适配器上没有 asyncTask,所以即使创建新的 thead 也是由 Activity 完成的(我认为这是在重置数字,但事实并非如此) .

现在已经坚持了 2 天,任何帮助将不胜感激。

【问题讨论】:

  • 当你说the "position" retrieved by getView不正确时,你的意思是“传入getView的位置”,还是发送给handler的消息中的位置?
  • 你想用处理程序实现什么? getView() 被动态调用以生成 ListView 中的行,生成行的顺序无关紧要。
  • 不保证调用getView的次数和顺序。所以这是一种预期的行为

标签: android listview android-listview baseadapter


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多