BaseExpandableListAdapter getChildView每个项目更新TextView

时间:2013-09-09 03:17:08

标签: android expandablelistview

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

我该如何解决这个问题?

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中的值是正确的。

1 个答案:

答案 0 :(得分:0)

我有类似的问题。我认为这是因为使用这种方法你将指针复制到你的对象loc,即使你认为你在一个循环中的值也是一样的, 我通过在你的案例loc

中对我的对象进行新的实例化来修复它

(ex loc = new loc

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