2016-02-28 19 views
0

我開發一個應用程序供人下載注意事項... 因此,有在每個Cardview 下載兩個按鈕和分享按鈕 下載按鈕和分享按鈕是混合...我的意思是..有時一個ViewHolder的數據用在另一個..有時當我點擊下載數據。從所有視圖的相同視圖下載。按鈕和ViewHolders聯繫日益混合

這裏是代碼...

package com.razorreborn.csebeta; 

import android.app.DownloadManager; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.CardView; 
import android.support.v7.widget.RecyclerView; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.webkit.CookieManager; 
import android.webkit.CookieSyncManager; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.TextView; 

import java.io.File; 
import java.util.List; 
/** 
* 
* Created by Kiran Anto aka RazorSharp on 2/17/2016. 
* For more Info Contact 
* [email protected] 
* 9495333724 
* All Copyrights Reserved 2016 
* 
*/ 
public class subjectCardAdapter extends RecyclerView.Adapter<subjectCardAdapter.ViewHolder> { 

    private final Context context; 
    private final List<subjectContent> subjectData; 
    DownloadManager downloadManager; 
    private String downloadFileUrl; 
    private long myDownloadReference; 
    private String type; 

    public subjectCardAdapter(List<subjectContent> subjectData, Context context){ 
     super(); 
     //Getting all the notification 
     this.subjectData = subjectData; 
     this.context = context; 
    } 
    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      View v = LayoutInflater.from(parent.getContext()) 
        .inflate(R.layout.list_subject, parent, false); 
      return new ViewHolder(v); 
    } 
    @Override 
    public void onBindViewHolder(final ViewHolder holder, int position) { 

     final subjectContent subjectContent = subjectData.get(position); 
     holder.Name.setText(subjectContent.getName()); 
     holder.Description.setText(subjectContent.getDescription()); 
     holder.Type.setText(subjectContent.getType()); 
     holder.Uploader.setText(subjectContent.getAuthor()); 
     holder.Date.setText(subjectContent.getDate()); 
     holder.Description.setMaxLines(5); 
     if(Global.Orientation.equals("Landscape")) { 
      holder.Name.setTextSize(20); 
      holder.Description.setTextSize(13); 
     } 
     final String DOWNLOAD_MESSAGE = "Download " + subjectContent.getName() +" of Subject " + Global.subject + " Notes from Here : " + downloadFileUrl + "\n And to Download this App : \n\n " + Global.AppShare; 
     downloadFileUrl = subjectContent.getDownload_link(); 
     downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); 
     type = downloadFileUrl.substring(downloadFileUrl.indexOf("Downloads/") + 10, downloadFileUrl.length()).replace("%20",""); 
     final String path = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+"/"+type; 
     Log.i("LOGS : ",path); 
     File applicationFile = new File (path); 
     if(applicationFile.exists()) { 
      holder.download.setText(" Open "); 
     } 
     holder.download.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // Add Content To download here...!!! 
       if (holder.download.getText().equals("Download")) { 
        Uri uri = Uri.parse(downloadFileUrl); 
        DownloadManager.Request request = new DownloadManager.Request(uri); 
        request.setTitle(subjectContent.getName()); 
          request.setDescription(subjectContent.getDescription()); 
        request.setDestinationInExternalFilesDir(context.getApplicationContext(),Environment.DIRECTORY_DOWNLOADS,type); 
        myDownloadReference = downloadManager.enqueue(request); 

        CookieSyncManager.createInstance(context); 
        CookieManager cookieManager = CookieManager.getInstance(); 
        cookieManager.removeAllCookie(); 
        /*Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this there would be crashing coz we dont call from a activity 
       context.startActivity(intent);*/ 
       } else if(holder.download.getText().equals(" Open ")) { 
        File file = new File (path); 
        Intent target = new Intent(Intent.ACTION_VIEW); 
        target.setDataAndNormalize(Uri.fromFile(file)); 
        String[] mimetypes = {"application/pdf","text/plain","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","application/vnd.openxmlformats-officedocument.wordprocessingml.document"}; 
        target.setType("application/pdf"); 
        target.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        target.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes); 
        Intent intent = Intent.createChooser(target, "Open File"); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        context.startActivity(intent); 
       } 
       } 
     }); 
     holder.share.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // Add Content To download here...!!! 
       Intent sharingIntent = new  Intent(android.content.Intent.ACTION_SEND); 
       sharingIntent.setType("text/plain"); 
       sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, DOWNLOAD_MESSAGE); 
       sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       Intent sharevia = new Intent(Intent.createChooser(sharingIntent, "Share via")); 
       sharevia.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       context.startActivity(sharevia); 
      } 
     }); 
     holder.cardView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //TODO implement onClick for the card here..! 
       holder.Description.setMaxLines(100); 
      } 
     }); 
    } 

    @Override 
    public int getItemCount() { 
     return subjectData.size(); 
    } 



    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 

     public final TextView Name; 
     public final TextView Description; 
     public final TextView Type; 
     public final TextView Uploader; 
     public final TextView Date; 
     public final Button download; 
     public final ImageButton share; 
     public final CardView cardView= (CardView) itemView.findViewById(R.id.card_view_subject); 
     //TODO cardview is taken to use it for onClick Events 
     public ViewHolder(View itemView) { 
      super(itemView); 

      Name = (TextView) itemView.findViewById(R.id.text_name); 
      Date = (TextView) itemView.findViewById(R.id.upload_date_blank); 
      Uploader = (TextView) itemView.findViewById(R.id.uploader_blank); 
      Description = (TextView) itemView.findViewById(R.id.text_description); 
      Type = (TextView) itemView.findViewById(R.id.textview_type); 
      download = (Button) itemView.findViewById(R.id.button); 
      share = (ImageButton) itemView.findViewById(R.id.sharebutton); 


      } 
      @Override 
      public void onClick(View view) { 
      } 
     } 
    } 
+0

您已在ViewHolder中實施了OnClickListener。你應該沒有點擊嗎?我在ViewHolder中看不到代碼download.setOnClickListener(this) – Raghunandan

回答

0

在recyclerview中,視圖被重用。你必須意識到,否則事情會混淆。當onBindViewHolder被調用時,你必須確保所有視圖中的屬性都被正確設置。

例如

if(applicationFile.exists()) { 
    holder.download.setText(" Open "); 
} 

值未設置在else分支到 「下載」。

+0

謝謝.. !!!無論如何,我解決了它...謝謝你的幫助。 –

0

現在你有你的private String downloadFileUrl;作爲成員變量。所以當你從你的onClickListener中引用它時,你指的是那個不能保證仍然是正確的文件URL的變量。

解決此問題的最簡單方法是在onClickListener中使用subjectContent.getDownload_link();。您通常不希望引用onClickListener中的成員變量,因爲它們將隨着新的ViewHolders的綁定而發生變化。

+0

謝謝.. !!!無論如何,我解決了它...謝謝你的幫助。 –