【发布时间】:2019-02-05 03:14:43
【问题描述】:
我正在使用 android studio 创建一个 whatsapp 状态下载工具。 在以下类文件中,这些行出现错误:
1> 视图 v = LayoutInflater.from(Parent.getContext()).inflate(R.layout.recyclerview_row_item,Parent,false); //错误是双亲下的红线说'无法解析符号'。
2> 文件 currentFile = filesList.get(Position); //错误是Redline无法解析符号'Position'
请帮帮我。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.FileHolder>
{
public final String DIR_SAVE = "/WSDownloader";
ArrayList<File> filesList;
Activity activity;
public MyAdapter(ArrayList<File> filesList, Activity activity)
{
this.filesList = filesList;
this.activity = activity;
setHasStableIds(true);
}
@NonNull
@Override
public FileHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(Parent.getContext()).inflate(R.layout.recyclerview_row_item,Parent,false);
return new FileHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final FileHolder fileHolder, int i)
{
File currentFile = filesList.get(Position);
fileHolder.imageDownload.setOnClickListener(this.downloadmediaitem(currentFile));
fileHolder.videoDownload.setOnClickListener(this.downloadmediaitem(currentFile));
if(currentFile.getAbsolutePath().endsWith(".mp4"))
{
fileHolder.ImageCardView.setVisibility(View.GONE);
fileHolder.VideoCardView.setVisibility(View.VISIBLE);
Uri videoUri = Uri.parse(currentFile.getAbsolutePath());
fileHolder.videoView.setVideoURI(videoUri);
fileHolder.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
fileHolder.videoView.start();
}
});
}
else
{
Bitmap bitmap = BitmapFactory.decodeFile(currentFile.getAbsolutePath());
fileHolder.imageView.setImageBitmap(bitmap);
}
}
@Override
public int getItemCount() {
return filesList.size();
}
public View.OnClickListener downloadmediaitem(final File sourceFile)
{
return new View.OnClickListener() {
@Override
public void onClick(View v) {
new Runnable()
{
@Override
public void run() {
File destfile = new File(Environment.getExternalStorageDirectory().toString()
+ DIR_SAVE + sourceFile.getName());
try {
copyFile(sourceFile,destfile);
} catch (IOException e) {
e.printStackTrace();
}
Snacky.builder().setActivty(activity)
.setText("Saved to Gallery")
.success()
.show();
}
}.run();
}
};
}
private void copyFile(File sourceFile, File destfile) throws IOException {
if(!destfile.getParentFile().exists())
{
destfile.getParentFile().mkdirs();
}
if(!destfile.exists())
destfile.createNewFile();
FileChannel source = null;
FileChannel destination = null;
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destfile).getChannel();
destination.transferFrom(source,0,source.size());
if(source!=null)
{
source.close();
}
if(destination!=null)
{
destination.close();
}
}
public static class FileHolder extends RecyclerView.ViewHolder
{
ImageView imageView;
VideoView videoView;
CardView ImageCardView,VideoCardView;
Button videoDownload,imageDownload;
public FileHolder(@NonNull View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.ImageViewMedia);
videoView = (VideoView) itemView.findViewById(R.id.VideoView);
ImageCardView = (CardView) itemView.findViewById(R.id.cardViewImage);
VideoCardView = (CardView) itemView.findViewById(R.id.VideoCardView);
videoDownload = (Button) itemView.findViewById(R.id.VideoDownloadButton);
imageDownload = (Button) itemView.findViewById(R.id.buttonDownloadImage);
}
}
}
【问题讨论】:
标签: android