0

我有一个可扩展的列表视图,我使用来自包含位置列表的 Track 对象的值填充该列表视图。它运行良好。但是,我的 TextView.setText 正在使用位置列表中的 LAST 值填充每个 textView。

我怎样才能解决这个问题?

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_child_item_layout, null);

            for (Location loc : trackList.get(groupPosition).getLocations()) {
                //Log.i(MY_EXPANDABLELIST_ACTIVITY_TAG, "Location X: " + loc.getLongitude());

                TextView textLongitude = (TextView) convertView.findViewById(R.id.textView_longitude);
                textLongitude.setText("Longitude: " + Double.toString(loc.getLongitude()));

                TextView textLatitude = (TextView) convertView.findViewById(R.id.textView_latitude);
                textLatitude.setText("Latitude: " + Double.toString(loc.getLatitude()));

                TextView textAltitude = (TextView) convertView.findViewById(R.id.textView_altitude);
                textAltitude.setText("Altitude: " + Double.toString(loc.getAltitude()));

                TextView textSpeed = (TextView) convertView.findViewById(R.id.textView_speed);
                textSpeed.setText("Speed: " + Float.toString(loc.getSpeed()));

                TextView textAccuracy = (TextView) convertView.findViewById(R.id.textView_accuracy);
                textAccuracy.setText("Accuracy: " + Float.toString(loc.getAccuracy()));

                SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.ENGLISH);
                Date date = new Date(loc.getTime());
                String formattedDate = format.format(date);
                TextView textTime = (TextView) convertView.findViewById(R.id.textView_time);
                textTime.setText("Time: " + formattedDate);
            }               
        }
        return convertView;
    }

LogCat 中的值是正确的。

4

1 回答 1

0

我有一个类似的问题。我认为这是因为使用这种方法,您将指针复制到您的对象 loc,即使您处于循环中,值也会相同,我通过在您的案例 loc 中创建我的对象的新实例来修复它

(ex loc = 新 loc

loc = tracklist.get(i) ) 类似的东西

于 2013-12-03T01:29:51.033 回答