如何使用 intent.putextra 设置 videoview 资源

How to set videoview resource using intent.putextra

我的原始文件夹中有一些 mp4 视频,现在我想使用 activity A 到 activity 的 intent.put 将这些视频传递到我的 videoview B 当用户点击一个项目时。

这是我的适配器 class:

public class absadapter extends RecyclerView.Adapter<absadapter.exViewHolder> {

List<abs.Ex> exList;
Context context;
String ex1 = "android.resource://" + context.getPackageName() + "/" + R.raw.ex1;


absadapter(List exList,Context ctx) {
    this.exList= exList;
    context=ctx;
}
public static class exViewHolder extends RecyclerView.ViewHolder{
    CardView cardView;
    TextView exname;
    ImageView exlogo;


    exViewHolder(View itemView){
        super(itemView);
        cardView= itemView.findViewById(R.id.cardView);
        exname= itemView.findViewById(R.id.ex_name);
        exlogo=itemView.findViewById(R.id.exlogo);



    }


}
@Override
public absadapter.exViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View viewthigh= LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_abslayout,parent,false);
    absadapter.exViewHolder evh=new absadapter.exViewHolder(viewthigh);
    return evh;


}


@Override
public void onBindViewHolder(final absadapter.exViewHolder holder, final int position){
    holder.exname.setText(exList.get(position).name);
    holder.exlogo.setImageResource(exList.get(position).logoId);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (position == 0 ){
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.crunches);
                context.startActivity(i1);


            }
            if (position == 1) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.raw.ex1);
                i1.putExtra("text",R.string.declinecrunch);
                context.startActivity(i1);


            }
            if (position == 2) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.dumsidebend);
                context.startActivity(i1);

            }
            if (position == 3) {
                Intent i1=new Intent(context,abdetails.class);

                i1.putExtra("text",R.string.hanglegraise);
                context.startActivity(i1);
                }
            if (position == 4) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.inclinelegraisegi);
                i1.putExtra("text",R.string.inclineleg);
                context.startActivity(i1);
                }
            if (position == 5) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.legraises);
                i1.putExtra("text",R.string.legraise);
                context.startActivity(i1);
            }
            if (position == 6) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.flatbenchlyingleg);
                context.startActivity(i1);
            }
            if (position == 7) {
                Intent i1=new Intent(context,abdetails.class);

                i1.putExtra("text",R.string.jackknife);
                context.startActivity(i1);
            }
            if (position == 8) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.jackknifegif);
                i1.putExtra("text",R.string.jackknife);
                context.startActivity(i1);
            }
            if (position == 9) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.twisthip);
                i1.putExtra("text",R.string.twisthip);
                context.startActivity(i1);
            }
            if (position == 10) {
                Intent i1=new Intent(context,abdetails.class);

                i1.putExtra("text",R.string.weightcrunch);
                context.startActivity(i1);
            }
            if (position == 11) {
                Intent i1=new Intent(context,abdetails.class);

                i1.putExtra("text",R.string.plank);
                context.startActivity(i1);
            }
            if (position == 12) {
                Intent i1=new Intent(context,abdetails.class);

                i1.putExtra("text",R.string.sideplank);
                context.startActivity(i1);
            }
            if (position == 13) {
                Intent i1=new Intent(context,abdetails.class);

                i1.putExtra("text",R.string.superman);
                context.startActivity(i1);
            }
            if (position == 14) {
                Intent i1=new Intent(context,abdetails.class);

                i1.putExtra("text",R.string.twistcrunch);
                context.startActivity(i1);
            }


        }
    });


}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView){
    super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
    return  exList.size();
}

我想在项目点击视频源的 bindviewholder 中执行所有代码应该设置:

这是我的主要内容 class:

public class abdetails extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_abdetails);
    VideoView videoView;
    videoView=findViewById(R.id.vdvw);
    Intent i1=getIntent();

    TextView tv;


    tv = findViewById(R.id.exdesc);
    tv.setMovementMethod(new ScrollingMovementMethod());
    tv.setText(i1.getIntExtra("text",0));

    }

你不应该通过整个视频。只需传递视频 ID(或视频名称,...),然后在 activity B 中,只需再次访问 raw 文件夹即可获取目标视频

在您的适配器中,传递位置:

@Override
public void onBindViewHolder(final Absadapter.ExViewHolder holder, final int position){
    ...
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i1=new Intent(context, Abdetails.class);
            i1.putExtra("POSITION", position);
            context.startActivity(i1);
        }
    }
    ...
}

然后在你的 AbDetailActivity 中:

public class AbDetailActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_abdetails);

        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle = getIntent().getExtras();
            int position = bundle.getInt("POSITION", 0);

            switch (position) {
                case 0:
                    // Load video 0
                    break;

                case 1:
                    // Load video 1
                    break:
                ...
        }
    }
}

我已经编辑了你的代码。希望它能解决你的问题。单击第一项时,下面的代码将播放您的视频文件。我的意思是当 position == 0;

public class absadapter extends RecyclerView.Adapter<absadapter.exViewHolder> {

List<abs.Ex> exList;
Context context;
String ex1 = "android.resource://" + context.getPackageName() + "/" + R.raw.ex1;

absadapter(List exList,Context ctx) {
    this.exList= exList;
    context=ctx;
}
public static class exViewHolder extends RecyclerView.ViewHolder{
    CardView cardView;
    TextView exname;
    ImageView exlogo;

    exViewHolder(View itemView){
        super(itemView);
        cardView= itemView.findViewById(R.id.cardView);
        exname= itemView.findViewById(R.id.ex_name);
        exlogo=itemView.findViewById(R.id.exlogo);

    }

}
@Override
public absadapter.exViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View viewthigh= LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_abslayout,parent,false);
    absadapter.exViewHolder evh=new absadapter.exViewHolder(viewthigh);
    return evh;
}

@Override
public void onBindViewHolder(final absadapter.exViewHolder holder, final int position){
    holder.exname.setText(exList.get(position).name);
    holder.exlogo.setImageResource(exList.get(position).logoId);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (position == 0 ){
                Intent i1=new Intent(context, abdetails.class);
                i1.putExtra("video", ex1);
                context.startActivity(i1);
            }
            if (position == 1) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.raw.ex1);
                i1.putExtra("text",R.string.declinecrunch);
                context.startActivity(i1);
            }
            if (position == 2) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.dumsidebend);
                context.startActivity(i1);
            }
            if (position == 3) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.hanglegraise);
                context.startActivity(i1);
                }
            if (position == 4) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.inclinelegraisegi);
                i1.putExtra("text",R.string.inclineleg);
                context.startActivity(i1);
                }
            if (position == 5) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.legraises);
                i1.putExtra("text",R.string.legraise);
                context.startActivity(i1);
            }
            if (position == 6) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.flatbenchlyingleg);
                context.startActivity(i1);
            }
            if (position == 7) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.jackknife);
                context.startActivity(i1);
            }
            if (position == 8) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.jackknifegif);
                i1.putExtra("text",R.string.jackknife);
                context.startActivity(i1);
            }
            if (position == 9) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.twisthip);
                i1.putExtra("text",R.string.twisthip);
                context.startActivity(i1);
            }
            if (position == 10) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.weightcrunch);
                context.startActivity(i1);
            }
            if (position == 11) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.plank);
                context.startActivity(i1);
            }
            if (position == 12) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.sideplank);
                context.startActivity(i1);
            }
            if (position == 13) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.superman);
                context.startActivity(i1);
            }
            if (position == 14) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.twistcrunch);
                context.startActivity(i1);
            }
        }
    });

}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView){
    super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
    return  exList.size();
}

这是您的视频播放器activity。

public class abdetails extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_abdetails);
    VideoView videoView;
    videoView=findViewById(R.id.vdvw);
    Intent i1=getIntent();
    TextView tv;

    tv = findViewById(R.id.exdesc);
    tv.setMovementMethod(new ScrollingMovementMethod());
    tv.setText(i1.getIntExtra("text",0));

    if(i1 != null){
       String path = i1.getStringExtra("video", "");
       videoView.setVideoURI(Uri.parse(path));
       videoView.start();
       videoView.setVisibility(View.VISIBLE);
    }

    }