I have a situation where I need to show the search bar in each line of recylerview to load media (audio). The file started successfully, but every time I click the downlaod button, only the last recylerview search bar does not work for the search associated with a particular line.
This is my adapter, where do I download audio from
public class ListenTestAdapter extends RecyclerView.Adapter<ListenTestAdapter.MyViewHolder> implements AdapterView.OnItemClickListener, View.OnClickListener, View.OnTouchListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnBufferingUpdateListener {
private static final int DOWNLOAD_THREAD_POOL_SIZE = 1;
private final Handler handler = new Handler();
public TextView testNameTextview, downloadTextView, seekBarTextView;
MyDownloadDownloadStatusListenerV1 myDownloadStatusListener = new MyDownloadDownloadStatusListenerV1();
int downloadId1;
private List<Listen> list = new ArrayList<>();
private Activity context;
private MediaPlayer mediaPlayer;
private SeekBar seekBarProgress;
private int mediaFileLengthInMilliseconds;
private String mp3Url;
private Handler durationHandler = new Handler();
private TextView duration;
private int timeElapsed;
private ThinDownloadManager downloadManager;
private String fileName;
private RetryPolicy retryPolicy ;
private File filesDir ;
private Uri downloadUri ;
private SeekBar seekBar ;
private Runnable updateSeekBarTime = new Runnable() {
public void run() {
timeElapsed = mediaPlayer.getCurrentPosition();
double timeRemaining = mediaFileLengthInMilliseconds - timeElapsed;
duration = (TextView) context.findViewById(R.id.duration);
duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));
durationHandler.postDelayed(this, 100);
}
};
public ListenTestAdapter(Activity context, List<Listen> list) {
this.list = list;
this.context = context;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(context, "You Clciked " + position, Toast.LENGTH_SHORT).show();
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
durationHandler.postDelayed(updateSeekBarTime, 100);
mediaFileLengthInMilliseconds = mediaPlayer.getDuration();
primarySeekBarProgressUpdater();
}
@Override
public void onClick(View v) {
}
@Override
public void onCompletion(MediaPlayer mp) {
context.findViewById(R.id.mediaPlayerLayout).setVisibility(View.GONE);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_listen_item, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
testNameTextview.setText(list.get(position).getPassages());
seekBar.setMax(100);
seekBar.setProgress(0);
downloadManager = new ThinDownloadManager(DOWNLOAD_THREAD_POOL_SIZE);
retryPolicy = new DefaultRetryPolicy();
filesDir = context.getExternalFilesDir(Environment.DIRECTORY_MUSIC);
downloadTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mp3Url = list.get(position).getQuestionGroupFile();
mp3Url = mp3Url.replace(" ", "%20");
if(!"".equals(mp3Url)){
downloadUri = Uri.parse(mp3Url);
fileName = mp3Url.substring(mp3Url.lastIndexOf('/') + 1);
downLoadAudio();
}else{
Toast.makeText(context, "Audio is not avialable", Toast.LENGTH_SHORT).show();
}
}
});
}
private void downLoadAudio() {
Uri destinationUri = Uri.parse(filesDir + "/" + fileName);
final DownloadRequest downloadRequest = new DownloadRequest(downloadUri)
.setDestinationURI(destinationUri).setPriority(DownloadRequest.Priority.LOW)
.setRetryPolicy(retryPolicy)
.setDownloadContext("Download1")
.setStatusListener(myDownloadStatusListener);
if (downloadManager.query(downloadId1) == DownloadManager.STATUS_NOT_FOUND) {
fileName = fileName.replace("%20", " ");
downloadId1 = downloadManager.add(downloadRequest);
} else {
Toast.makeText(context, "Please wait....Downloading is in progress", Toast.LENGTH_SHORT).show();
}
}
private void primarySeekBarProgressUpdater() {
seekBarProgress.setProgress((int) (((float) mediaPlayer.getCurrentPosition() / mediaFileLengthInMilliseconds) * 100));
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
primarySeekBarProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
}
}
@Override
public int getItemCount() {
return list.size();
}
private String getBytesDownloaded(int progress, long totalBytes) {
long bytesCompleted = (progress * totalBytes) / 100;
if (totalBytes >= 1000000) {
return ("" + (String.format("%.1f", (float) bytesCompleted / 1000000)) + "/" + (String.format("%.1f", (float) totalBytes / 1000000)) + "MB");
}
if (totalBytes >= 1000) {
return ("" + (String.format("%.1f", (float) bytesCompleted / 1000)) + "/" + (String.format("%.1f", (float) totalBytes / 1000)) + "Kb");
} else {
return ("" + bytesCompleted + "/" + totalBytes);
}
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View view) {
super(view);
testNameTextview = (TextView) view.findViewById(R.id.testNameTextview);
downloadTextView = (TextView) view.findViewById(R.id.downloadTextview);
seekBarTextView = (TextView) context.findViewById(R.id.duration);
seekBar = (SeekBar) view.findViewById(R.id.seekBar);
}
}
class MyDownloadDownloadStatusListenerV1 implements DownloadStatusListenerV1 {
@Override
public void onDownloadComplete(DownloadRequest request) {
final int id = request.getDownloadId();
if (id == downloadId1) {
seekBarTextView.setText("Downaloaded" + " Audio: " + fileName + " Completed");
EncryptDownloadedAudio.encrypt(context,mp3Url,fileName);
}
}
@Override
public void onDownloadFailed(DownloadRequest request, final int errorCode, final String errorMessage) {
final int id = request.getDownloadId();
if (id == downloadId1) {
context.runOnUiThread(new Runnable() {
@Override
public void run() {
seekBarTextView.setText("Failed to download: " + fileName + " Failed: ErrorCode " + errorCode + ", " + errorMessage);
seekBar.setProgress(0);
}
});
}
}
@Override
public void onProgress(DownloadRequest request, long totalBytes, long downloadedBytes, int progress) {
int id = request.getDownloadId();
System.out.println("######## onProgress ###### " + id + " : " + totalBytes + " : " + downloadedBytes + " : " + progress);
if (id == downloadId1) {
seekBarTextView.setText("Downloading: " + fileName + ", " + progress + "%" + " " + getBytesDownloaded(progress, totalBytes));
seekBar.setProgress(progress);
}
}
}
}