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

时间:2019-04-08 10:21:38

标签: android firebase firebase-realtime-database android-recyclerview recycler-adapter

图片:

enter image description here

我正在将firebase用于视频状态android应用程序,并使用回收站视图,但是firebase daat不会将第一个活动传递给另一个(第二个)活动,我如何解决此问题(错误)
空对象引用上出现错误Model.videolink”。

应用专家

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

1 个答案:

答案 0 :(得分:0)

尝试此代码。 使接口进入用于单击处理的适配器类。

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