Update progressbar in Gridview

79 views Asked by At

I have AudioListFile with Player Program. When i click an item of GridView, sound will be played.! enter image description here

Now, i wonder, How can i update ProgressBar in GridView? I need mediaplayer.getDuration() in adapter. please help me. thank you so much. My adapter:

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

    if (convertView == null) {

        // Initialize views
        holder = new Holder();
        convertView = inflater.inflate(R.layout.raw_audio_list_item, null);
        holder.tvFileName = (TextView) convertView.findViewById(R.id.row_audio_list_item_tv_file_name);
        holder.tvFileSize = (TextView) convertView.findViewById(R.id.row_audio_list_item_tv_file_size);
        holder.pbPlayProgress = (ProgressBar) convertView.findViewById(R.id.audio_file_list_pb);

        convertView.setTag(holder);


    } else {

        holder = (Holder) convertView.getTag();


    }

    // get the map object from list
    AudioDataModel audioFileModel = audioFileList.get(position);

    // set data to view
    holder.tvFileName.setText(audioFileModel.getFileName());
    holder.tvFileSize.setText(audioFileModel.getFileSize());
    holder.pbPlayProgress.setMax(Integer.parseInt(audioFileModel.getFileDuration()));

    convertView.setId(Integer.valueOf(audioFileModel.getFileId()));

    return convertView;
}
1

There are 1 answers

0
mcd On

actually you can easily do this but you have to use sparseIntegerHashmap to track the current progress for particular view because gridview or listview recycle the view they reuse the view for efficiency. so first check in getview method check whether this particular view are for this particular progress bar (means it not reused ) then set it's value from hashmap and if this view is newly created (no entry found in hashmap then) please apply default value for progress bar . simple you have to track individual view with progress in hashmap . current code might not be working because i am not on my workstation right now hope you will understand e.g

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

    if (convertView == null) {

        // Initialize views
        holder = new Holder();
        convertView = inflater.inflate(R.layout.raw_audio_list_item, null);
        holder.tvFileName = (TextView) convertView.findViewById(R.id.row_audio_list_item_tv_file_name);
        holder.tvFileSize = (TextView) convertView.findViewById(R.id.row_audio_list_item_tv_file_size);
        holder.pbPlayProgress = (ProgressBar) convertView.findViewById(R.id.audio_file_list_pb);

        convertView.setTag(holder);


    } else {

        holder = (Holder) convertView.getTag();


    }

    // get the map object from list
    AudioDataModel audioFileModel = audioFileList.get(position);

    // set data to view
    holder.tvFileName.setText(audioFileModel.getFileName());
    holder.tvFileSize.setText(audioFileModel.getFileSize());    holder.pbPlayProgress.setMax(Integer.parseInt(audioFileModel.getFileDuration()));

    convertView.setId(Integer.valueOf(audioFileModel.getFileId()));
      //your unique id for compare  
     if(map.get(audioFileModel.getFileId())==holder.getId) {
             holder.pbPlayProgress.setProgress(map.get(audioFileModel.getFileId()));
     } else {
      holder.pbPlayProgress.setProgress(//add default value );
      // also add entry in hashmap 
       map.add(audioFileModel.getFileId(),default value)
     }
    return convertView;
}