列表视图中的重复元素

时间:2013-08-26 17:10:43

标签: android android-listview

我突然在listview中获得重复的元素。当我对列表项上的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;
    }

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

我认为你在那里遇到了竞争条件。您在convertView初始化之前返回AsyncTask。请注意,return convertView可能会在调用doInBackground之前执行。

我建议你在一个AsyncTask中加载所有视图数据,或者等到调用postExecute才能返回convertView。或者,如果工作量不是很慢,则不要使用AsyncTask