0

我突然在我的列表视图中得到重复的元素。当我对列表项进行 onClick 操作时,一切都恢复正常。我还检查了我的适配器,我没有在 ArrayList 中添加多个项目。这里可能有什么问题。

public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null || convertView.getTag() == null) {
            convertView = inflater.inflate(R.layout.bookingresponse_row, null);
            final CabbieViewHolder viewHolder = new CabbieViewHolder();
            viewHolder.call = (ImageView) convertView.findViewById(R.id.callCab);
            viewHolder.call.setOnClickListener(this);
            viewHolder.driverName = (TextView) convertView.findViewById(R.id.driverName);
            //SIMILAR CODE FOR MORE DATA ITEMs

            convertView.setTag(viewHolder);
        }
        // Setting all values in listview
        String temp = latestAcceptedCabbieName.get(position);
        ((CabbieViewHolder) (convertView.getTag())).driverName.setText(temp);
        ((CabbieViewHolder) (convertView.getTag())).inDetailsDriverName.setText(temp);

        //SIMIAR CODE for other data items
        // Smoothen out the list
        new AsyncTask<Object, Void, Float>() {
            CabbieViewHolder cabbieViewHolder;

            @Override
            protected Float doInBackground(Object... params) {
                float cabDistanceFromCustomer = -1.0f;
                cabbieViewHolder = (CabbieViewHolder) params[0];
                if (cabbieViewHolder.distanceFromCustomer == -1.0f) {
                    int position = (int) ((Integer) params[1]).intValue();
                    cabbieViewHolder.distanceFromCustomer = cabDistanceFromCustomer = God
                            .getGeoDistance(Double.valueOf(latestAcceptedCabbieSourceGPSX
                                    .get(position)), Double
                                    .valueOf(latestAcceptedCabbieSourceGPSY.get(position)),
                                    Double.valueOf(customerSourceLat / 1E6), Double
                                            .valueOf(customerSourceLng / 1E6));
                } else
                    cabDistanceFromCustomer = cabbieViewHolder.distanceFromCustomer;
                return cabDistanceFromCustomer;
            }

            @Override
            protected void onPostExecute(Float result) {
                super.onPostExecute(result);
                if (customerSourceLat != 0 && customerSourceLng != 0) {
                    String temp;
                    if (result != 0) {
                        temp = "Current Location @ Approx "
                                + String.format("%.1f", result / 1000) + " km";
                        cabbieViewHolder.cabDistance.setText(temp);
                        cabbieViewHolder.inDetailsCabDistance.setText(temp);
                    } else {
                        cabbieViewHolder.cabDistance.setVisibility(View.GONE);
                        cabbieViewHolder.inDetailsCabDistance.setVisibility(View.GONE);
                    }
                } else {
                    cabbieViewHolder.cabDistance.setVisibility(View.GONE);
                    cabbieViewHolder.inDetailsCabDistance.setVisibility(View.GONE);
                }
            }
        }.execute((CabbieViewHolder) convertView.getTag(), new Integer(position));
        return convertView;
    }
4

2 回答 2

1

我看到您正在异步任务中加载内容。如果任务尚未完成,您将看到旧内容。

尝试在视图中放置一个“正在加载数据”指示器,这样您就知道数据在当 convertView 不为空时执行的 else 块中是陈旧的。

于 2013-08-26T17:19:29.273 回答
1

我认为你在那里有比赛条件。你convertView在它被初始化之前返回AsyncTask。请记住,return convertView可能会在doInBackground调用之前执行。

我建议您要么将所有视图数据加载到一个中AsyncTask,要么等到postExecute调用返回convertView. AsyncTask或者,如果工作负载不是很慢,则根本不使用。

于 2013-08-26T17:20:35.497 回答