【问题标题】:Parent.getContext() and filesList.get(Position) shows errorParent.getContext() 和 filesList.get(Position) 显示错误
【发布时间】: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


    【解决方案1】:

    ParentPosition 未定义!您必须使用“viewGroup”更改父级,使用“i”更改位置... 我认为您从网络或指南中获取代码,但以不同的方式调用参数。您的错误告诉您编译器找不到变量名。

    代码需要是:

    @NonNull
    @Override
    public FileHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.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(i);
    }
    

    希望对您有所帮助:D

    【讨论】:

    • 是的,现在所有的错误都消失了,谢谢...但是该项目根本不起作用:P
    • 我们解决了这些错误.. 所以,你现在可以打开另一个主题,新问题大声笑:P
    • 真的,现在就做
    【解决方案2】:

    我会做以下更改。

    1) Activity activityContext context

    2)public MyAdapter(ArrayList<File> filesList, Context context) {this.filesList = filesList; this.context = context; setHasStableIds(true); }

    3) 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); }

    public FileHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new FileHolder(LayoutInflater.from(context).
                inflate(R.layout.recyclerview_item, viewGroup, false));
    };
    

    【讨论】:

    • 你是否将“Activity 活动”更改为“上下文上下文”并使用 Context 实例化了适配器?
    猜你喜欢
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 2017-07-16
    相关资源
    最近更新 更多