Search code examples
androidandroid-arrayadapterandroid-seekbar

Multiple SeekBars in Adapter, only last SeekBar updates


I have a ListView which has a SeekBar in each row. the size of the list and therefore the number of SeekBars will vary.

I've set a SeekBar.OnSeekBarChangeListener and from that i update a TextView based on the integer the SeekBar is displaying.

The problem is that only the last SeekBar in the list seems to update from the SeekBar.OnSeekBarChangeListener.

How can i make all of the SeekBars work?

Thanks in advance.

private class MySimpleArrayAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final ArrayList<?> list;            

        public MySimpleArrayAdapter(Context context, ArrayList<?> list) {

            super(context, R.layout.monitoringrowlayout);
            Log.e(TAG, "inside adapter constructor");
            this.context = context;
            this.list = list;               

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {    

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View rowView = inflater.inflate(R.layout.monitoringrowlayout, parent, false);

            final int pos = position;               

            Log.e(TAG, "list size in adapter = " + list.size());

            String record = list.get(position).toString();
            rowView.setTag(record);
            String[] itemsInRecord = record.split(",");
            Log.e(TAG, "itemin record = " + itemsInRecord.length);
            String[] recordItem = new String[itemsInRecord.length];

            for (int x = 0; x < itemsInRecord.length; x++) {

                recordItem[x] = itemsInRecord[x];

                Log.e(TAG, "recordItem[x]" + x + " " +recordItem[x] );

            }

            recordItem[0] = recordItem[0].replace('[', ' ').trim();     
            recordItem[3] = recordItem[3].replace(']', ' ').trim(); 

            TextView description =  (TextView) rowView.findViewById(R.id.rowmonitoringdescription);
            description.setText(recordItem[1]);

            rating =  (TextView) rowView.findViewById(R.id.rowmonitoringrating);                

            sb = (SeekBar)rowView.findViewById(R.id.seekbarmonitoring);
            sb.setOnSeekBarChangeListener(customSeekBarListener);

            return rowView;

        }

        @Override
        public int getCount() {
            return this.list.size();
        }

    }// end of adapter class

private SeekBar.OnSeekBarChangeListener customSeekBarListener = new SeekBar.OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        upDateTextBox(progress);

    }
};    

private void upDateTextBox(int progress){

    rating.setText(String.valueOf(progress));       

}

Solution

  • This was obviously going to happen.

    Since sb will always be the "last" SeekBar, and rating will be always the "last" rating TextView.

    Change your code:

        private class MySimpleArrayAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final ArrayList<?> list;
    
    
        public MySimpleArrayAdapter(Context context, ArrayList<?> list) {
    
            super(context, R.layout.monitoringrowlayout);
            Log.e(TAG, "inside adapter constructor");
            this.context = context;
            this.list = list;
    
    
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
    
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            View rowView = inflater.inflate(R.layout.monitoringrowlayout, parent, false);
    
            final int pos = position;
    
    
            Log.e(TAG, "list size in adapter = " + list.size());
    
    
    
            String record = list.get(position).toString();
            rowView.setTag(record);
            String[] itemsInRecord = record.split(",");
            Log.e(TAG, "itemin record = " + itemsInRecord.length);
            String[] recordItem = new String[itemsInRecord.length];
    
            for (int x = 0; x < itemsInRecord.length; x++) {
    
                recordItem[x] = itemsInRecord[x];
    
                Log.e(TAG, "recordItem[x]" + x + " " +recordItem[x] );
    
            }
    
            recordItem[0] = recordItem[0].replace('[', ' ').trim();
            recordItem[3] = recordItem[3].replace(']', ' ').trim();
    
            TextView description =  (TextView) rowView.findViewById(R.id.rowmonitoringdescription);
            description.setText(recordItem[1]);
    
    
    
            SeekBar sb = (SeekBar)rowView.findViewById(R.id.seekbarmonitoring);
            sb.setOnSeekBarChangeListener(customSeekBarListener);
            sb.setTag(rowView.findViewById(R.id.rowmonitoringrating));
    
    
    
    
    
    
            return rowView;
    
    
    
    
        }
    
        @Override
        public int getCount() {
            return this.list.size();
        }
    
    }// end of adapter class
    
    
    
        private SeekBar.OnSeekBarChangeListener customSeekBarListener = new SeekBar.OnSeekBarChangeListener() {
    
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    
                upDateTextBox((TextView) seekBar.getTag(), progress);
    
            }
        };
    
    
        private void upDateTextBox(TextView tv, int progress){
    
            tv.setText(String.valueOf(progress));
    
    
        }
    }