使用firebase将数据回收器适配器传递给其他活动

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用firebase将数据回收器适配器传递给其他活动相关的知识,希望对你有一定的参考价值。

图片 :

我正在使用firebase用于视频状态android应用程序,并使用recycleler视图但firebase daat不会将第一个活动传递给另一个(第二个)活动,我怎么能解决这个问题(错误) null对象引用上的错误Model.videolink'。

App Adepter

public class App_Adepter extends RecyclerView.Adapter<App_Adepter.ViewHolder> {

Model model;
private Context context;
private ArrayList<Model> uploads;
private ProgressDialog pDialog;
private MediaController mediaController;

class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView imageView;
    public VideoView videoView;
    public TextView textView;

    public ViewHolder(View itemView) {
        super(itemView);
        imageView = (ImageView) itemView.findViewById(R.id.rImageView);
        videoView = (VideoView) itemView.findViewById(R.id.test_video);
        textView = (TextView) itemView.findViewById(R.id.text11);
    }
}

public App_Adepter(Context context, ArrayList<Model> uploads) {
    this.uploads = uploads;
    this.context = context;
}

@Override
public App_Adepter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.row, parent, false);
    return new App_Adepter.ViewHolder(v);
}

@Override
public void onBindViewHolder(final App_Adepter.ViewHolder holder, final int position) {
    final Model upload = uploads.get(position);


    /*Video Click*/
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, Full_video.class);
            intent.putExtra("posss", model.image);
            intent.putParcelableArrayListExtra("Category", uploads);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);

        }
    });

    final MediaController mediaController = new MediaController(context);

    String uriPath = upload.getImage();  //update package name
    Uri uri = Uri.parse(uriPath);


    holder.textView.setText(upload.getVideolink());
    Glide.with(context).load(upload.getImage()).into(holder.imageView);


    holder.videoView.setVideoURI(uri);

    holder.videoView.start();

    holder.videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            return true;
        }
    });

    holder.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                @Override
                public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

                    /**** Media control coding ****///
                   holder.videoView.setMediaController(mediaController);
                   mediaController.setAnchorView(holder.videoView);
                    mp.setVolume(0, 0);

                }
            });
        }
    });

}


@Override
public long getItemId(int position) {
    return position;
}

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

}

模型类

String image;
String videolink;

public Model() {
}

public static Creator<Model> getCREATOR() {

    return CREATOR;
}

public Model(String image,String videolink) {

    this.videolink = videolink;
    this.image = image;
}

protected Model(Parcel in) {
    image = in.readString();
    videolink = in.readString();
}

public static final Creator<Model> CREATOR = new Creator<Model>() {
    @Override
    public Model createFromParcel(Parcel in) {
        return new Model(in);
    }

    @Override
    public Model[] newArray(int size) {
        return new Model[size];
    }
};

public String getVideolink() {
    return videolink;
}

public void setVideolink(String videolink) {
    this.videolink = videolink;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(image);
    dest.writeString(videolink);
}

其他(第二)活动

    Bundle extra = getIntent().getExtras();
    String value = extra.getString("posss");

    videoView.setVideoURI(Uri.parse(value));

第一项活动

    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            progressDialog.dismiss();

            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                Model upload = postSnapshot.getValue(Model.class);
                app_models.add(upload);
            }

            adapter = new App_Adepter(App_screen.this, app_models);
            recyclerView.setAdapter(adapter);

            if (app_models.size() <= 0) {
                textView.setVisibility(View.VISIBLE);
            } else {
                textView.setVisibility(View.GONE);
            }
        }
答案

试试这个代码..将接口转换为适配器类,单击handel ..

onItemClick onItemClick;

public void setOnItemClick(adapterDemo.onItemClick onItemClick) {
    this.onItemClick = onItemClick;
}

interface onItemClick{
    void onClickData(Model data);
}

然后onBindView方法改变..

    @Override
public void onBindViewHolder(@NonNull adapterDemo.ViewHolder viewHolder, int i) {

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onItemClick.onClickData(upload);
        }
    });
}

之后在活动中定义适配器并绑定到recyclerview ..确保您的适配器对象不为空..

adapterDemo.setOnItemClick(new adapterDemo.onItemClick() {
        @Override
        public void onClickData(Model data) {
            // here get click object data..
            Intent intent = new Intent(context, Full_video.class);
            intent.putExtra("posss", model.image);
            intent.putParcelableArrayListExtra("Category", uploads);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    });

以上是关于使用firebase将数据回收器适配器传递给其他活动的主要内容,如果未能解决你的问题,请参考以下文章

如何根据位置从“回收”视图中删除Firebase数据库密钥

从火力基地查询的数据不会显示在recyclerview和grid视图中

使用firebase在无尽的回收器视图中进行分页

通过firebase auth添加图片和其他数据

FireBase 实时数据库:回收站视图未填充

我无法在 Firebase 中获得孩子的钥匙